-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
把包名map_must和slice_must改为mustmap和mustslice以保持符合规范和语境
- Loading branch information
yangyile
committed
Nov 28, 2024
1 parent
8bda865
commit fea23ee
Showing
7 changed files
with
258 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mustmap | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/yyle88/zaplog" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Equals compares two maps for equality. If they are not equal, it panics. | ||
// Equals 比较两个 map 是否相等,如果不相等,则触发 panic。 | ||
func Equals[K, V comparable](a, b map[K]V) { | ||
if !maps.Equal(a, b) { | ||
zaplog.ZAPS.P1.LOG.Panic("expect map equals while not equals", zap.Int("len_a", len(a)), zap.Int("len_b", len(b))) | ||
} | ||
} | ||
|
||
// Have checks if a map is non-empty. If it is empty, it panics. | ||
// Have 检查一个 map 是否非空,如果为空,则触发 panic。 | ||
func Have[K comparable, V any](a map[K]V) { | ||
if len(a) == 0 { | ||
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH > 0 while got an none map") | ||
} | ||
} | ||
|
||
// Nice checks if a map is non-empty and returns it. If it is empty, it panics. | ||
// Nice 检查一个 map 是否非空并返回它,如果为空,则触发 panic。 | ||
func Nice[K comparable, V any](a map[K]V) map[K]V { | ||
if len(a) == 0 { | ||
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH > 0 while got an none map") | ||
} | ||
return a | ||
} | ||
|
||
// Length checks if the length of a map is equal to n. If not, it panics. | ||
// Length 检查一个 map 的长度是否等于 n,如果不等,则触发 panic。 | ||
func Length[K comparable, V any](a map[K]V, n int) { | ||
if len(a) != n { | ||
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH = n while not equals", zap.Int("len", len(a)), zap.Int("n", n)) | ||
} | ||
} | ||
|
||
// Len checks if the length of a map is equal to n. If not, it panics. | ||
// Len 是 Length 的简写版本,检查一个 map 的长度是否等于 n,如果不等,则触发 panic。 | ||
func Len[K comparable, V any](a map[K]V, n int) { | ||
if len(a) != n { | ||
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH = n while not equals", zap.Int("len", len(a)), zap.Int("n", n)) | ||
} | ||
} | ||
|
||
// Get func get value of key from the map. If the key does not exist, it panics. | ||
// Get 根据给定的键从 map 中检索值,如果键不存在,则触发 panic。 | ||
func Get[K, V comparable](a map[K]V, key K) V { | ||
value, exists := a[key] | ||
if !exists { | ||
zaplog.ZAPS.P1.LOG.Panic("expect KEY to EXIST in map", zap.Any("key", key)) | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package mustmap_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/must/internal/tests" | ||
"github.com/yyle88/must/mustmap" | ||
) | ||
|
||
func TestEquals(t *testing.T) { | ||
mustmap.Equals(map[int]string{ | ||
1: "a", | ||
2: "b", | ||
}, map[int]string{ | ||
2: "b", | ||
1: "a", | ||
}) | ||
|
||
tests.ExpectPanic(t, func() { | ||
mustmap.Equals(map[string]int{ | ||
"a": 1, | ||
}, map[string]int{ | ||
"b": 2, | ||
}) | ||
}) | ||
} | ||
|
||
func TestHave(t *testing.T) { | ||
// 正常情况: 非空 map 不应触发 panic | ||
mustmap.Have(map[int]string{ | ||
1: "value1", | ||
2: "value2", | ||
}) | ||
|
||
// 异常情况: 空 map 应触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Have(map[string]int{}) | ||
}) | ||
} | ||
|
||
func TestNice(t *testing.T) { | ||
require.Equal(t, map[string]int{"a": 1, "b": 2, "c": 3}, mustmap.Nice(map[string]int{"c": 3, "b": 2, "a": 1})) | ||
|
||
tests.ExpectPanic(t, func() { | ||
mustmap.Nice(map[string]int{}) | ||
}) | ||
} | ||
|
||
func TestLength(t *testing.T) { | ||
// 正常情况: map 的长度等于期望值 | ||
mustmap.Length(map[int]string{ | ||
1: "value1", | ||
2: "value2", | ||
}, 2) | ||
|
||
// 异常情况: map 的长度不等于期望值 | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Length(map[int]string{ | ||
1: "value1", | ||
}, 2) | ||
}) | ||
|
||
// 边界情况: 空 map 长度为 0 | ||
mustmap.Length(map[string]int{}, 0) | ||
|
||
// 异常情况: 非空 map 长度与期望值不符 | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Length(map[string]int{"a": 1, "b": 2}, 3) | ||
}) | ||
} | ||
|
||
func TestLen(t *testing.T) { | ||
// 正常情况: Len 的行为与 Length 相同 | ||
mustmap.Len(map[int]string{ | ||
1: "value1", | ||
2: "value2", | ||
}, 2) | ||
|
||
// 异常情况: 长度不符触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Len(map[int]string{ | ||
1: "value1", | ||
}, 2) | ||
}) | ||
|
||
// 边界情况: 空 map 长度为 0 | ||
mustmap.Len(map[string]int{}, 0) | ||
|
||
// 异常情况: 非空 map 长度与期望值不符 | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Len(map[string]int{"a": 1, "b": 2}, 3) | ||
}) | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
// 测试键存在时,返回对应的值 | ||
value := mustmap.Get(map[string]int{"a": 1, "b": 2}, "a") | ||
require.Equal(t, 1, value) | ||
|
||
// 测试键不存在时,触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustmap.Get(map[string]int{"a": 1, "b": 2}, "c") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package mustslice_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/must/internal/tests" | ||
"github.com/yyle88/must/mustslice" | ||
) | ||
|
||
func TestEquals(t *testing.T) { | ||
// 正常情况下,两个切片相等 | ||
mustslice.Equals([]int{1, 2, 3}, []int{1, 2, 3}) | ||
|
||
// 切片不相等时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Equals([]string{"a"}, []string{"b"}) | ||
}) | ||
} | ||
|
||
func TestContains(t *testing.T) { | ||
// 切片包含指定元素时通过 | ||
mustslice.Contains([]string{"a", "b", "c"}, "a") | ||
|
||
// 切片不包含指定元素时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Contains([]int{1, 2, 3}, 4) | ||
}) | ||
} | ||
|
||
func TestIn(t *testing.T) { | ||
// 元素在切片中时通过 | ||
mustslice.In("a", []string{"a", "b", "c"}) | ||
|
||
// 元素不在切片中时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.In(4, []int{1, 2, 3}) | ||
}) | ||
} | ||
|
||
func TestNice(t *testing.T) { | ||
// 非空切片返回自身 | ||
require.Equal(t, []int{1, 2, 3}, mustslice.Nice([]int{1, 2, 3})) | ||
|
||
// 空切片时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Nice([]string{}) | ||
}) | ||
} | ||
|
||
func TestHave(t *testing.T) { | ||
// 非空切片通过 | ||
mustslice.Have([]int{1, 2, 3}) | ||
|
||
// 空切片触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Have([]string{}) | ||
}) | ||
} | ||
|
||
func TestLength(t *testing.T) { | ||
// 切片长度符合期望值时通过 | ||
mustslice.Length([]int{1, 2, 3}, 3) | ||
|
||
// 切片长度不符合期望值时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Length([]string{"a", "b"}, 3) | ||
}) | ||
} | ||
|
||
func TestLen(t *testing.T) { | ||
// Len 与 Length 作用相同,测试该函数的行为 | ||
mustslice.Len([]int{1, 2, 3}, 3) | ||
|
||
// 切片长度不符合期望值时触发 panic | ||
tests.ExpectPanic(t, func() { | ||
mustslice.Len([]string{"a", "b"}, 3) | ||
}) | ||
} |
Oops, something went wrong.