diff --git a/map_must/map_must.go b/map_must/map_must.go deleted file mode 100644 index a319f83..0000000 --- a/map_must/map_must.go +++ /dev/null @@ -1,45 +0,0 @@ -package map_must - -import ( - "maps" - - "github.com/yyle88/zaplog" - "go.uber.org/zap" -) - -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 意思是 NotEmpty 非空 map -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 意思是 NotEmpty 非空 map,当传入有元素的map时返回它 -// must.Nice(a) 的作用仅仅是判定是否为空,而这里的作用是判断是否有内容,但这样易混淆,因此建议使用 Have 函数 -// 当你确实需要既断言有元素,而且还要立即使用它时,也可以用这个函数。 -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 期望长度是 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 和 Length 作用相同,只是缩写,我更倾向于使用缩写 -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)) - } -} diff --git a/map_must/map_must_test.go b/map_must/map_must_test.go deleted file mode 100644 index 3644316..0000000 --- a/map_must/map_must_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package map_must_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/yyle88/must/internal/tests" - "github.com/yyle88/must/map_must" -) - -func TestEquals(t *testing.T) { - map_must.Equals(map[int]string{ - 1: "a", - 2: "b", - }, map[int]string{ - 2: "b", - 1: "a", - }) - - tests.ExpectPanic(t, func() { - map_must.Equals(map[string]int{ - "a": 1, - }, map[string]int{ - "b": 2, - }) - }) -} - -func TestNice(t *testing.T) { - require.Equal(t, map[string]int{"a": 1, "b": 2, "c": 3}, map_must.Nice(map[string]int{"c": 3, "b": 2, "a": 1})) - - tests.ExpectPanic(t, func() { - map_must.Nice(map[string]int{}) - }) -} diff --git a/mustmap/must_map.go b/mustmap/must_map.go new file mode 100644 index 0000000..04ba75f --- /dev/null +++ b/mustmap/must_map.go @@ -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 +} diff --git a/mustmap/must_map_test.go b/mustmap/must_map_test.go new file mode 100644 index 0000000..1df9202 --- /dev/null +++ b/mustmap/must_map_test.go @@ -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") + }) +} diff --git a/slice_must/slice_must.go b/mustslice/must_slice.go similarity index 54% rename from slice_must/slice_must.go rename to mustslice/must_slice.go index 3d497e4..09f84e4 100644 --- a/slice_must/slice_must.go +++ b/mustslice/must_slice.go @@ -1,4 +1,4 @@ -package slice_must +package mustslice import ( "slices" @@ -7,36 +7,40 @@ import ( "go.uber.org/zap" ) +// Equals checks if two slices are equal, panics if not. +// Equals 检查两个切片是否相等,不相等则触发 panic。 func Equals[V comparable](a, b []V) { if !slices.Equal(a, b) { zaplog.ZAPS.P1.LOG.Panic("expect slice equals while not equals", zap.Int("len_a", len(a)), zap.Int("len_b", len(b))) } } -// In 检查元素是否在数组里 +// In checks if an element exists in a slice, panics if not. +// In 检查某个元素是否存在于切片中,不存在则触发 panic。 func In[T comparable](v T, a []T) { if !slices.Contains(a, v) { zaplog.ZAPS.P1.LOG.Panic("expect value in slice while not in", zap.Any("v", v), zap.Int("len", len(a))) } } -// Contains 检查数组是否包含元素,假如不包含就 panic +// Contains checks if a slice contains a specific element, panics if not. +// Contains 检查切片是否包含某个特定元素,不包含则触发 panic。 func Contains[T comparable](a []T, v T) { if !slices.Contains(a, v) { zaplog.ZAPS.P1.LOG.Panic("expect slice contains value while not contains", zap.Int("len", len(a)), zap.Any("v", v)) } } -// Have 意思是 NotEmpty 非空 slice +// Have ensures the slice is not empty, panics if it is. +// Have 确保切片不为空,为空则触发 panic。 func Have[T any](a []T) { if len(a) == 0 { zaplog.ZAPS.P1.LOG.Panic("expect LENGTH > 0 while got an none slice") } } -// Nice 意思是 NotEmpty 非空 slice,当传入有元素的slice时返回它 -// must.Nice(a) 的作用仅仅是判定是否为空,而这里的作用是判断是否有内容,但这样易混淆,因此建议使用 Have 函数 -// 当你确实需要既断言有元素,而且还要立即使用它时,也可以用这个函数。 +// Nice ensures the slice is not empty, and returns it if it contains elements. +// Nice 确保切片不为空,若切片有元素则返回它。 func Nice[T any](a []T) []T { if len(a) == 0 { zaplog.ZAPS.P1.LOG.Panic("expect LENGTH > 0 while got an none slice") @@ -44,14 +48,16 @@ func Nice[T any](a []T) []T { return a } -// Length 期望长度是 n,否则 panic +// Length checks if the slice's length equals the expected value, panics if not. +// Length 检查切片的长度是否等于期望值,不等则触发 panic。 func Length[T any](a []T, 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 和 Length 作用相同,只是缩写,我更倾向于使用缩写 +// Len checks if the slice's length equals the expected value, panics if not. +// Len 检查切片的长度是否等于期望值,不等则触发 panic。 func Len[T any](a []T, 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)) diff --git a/mustslice/must_slice_test.go b/mustslice/must_slice_test.go new file mode 100644 index 0000000..df04516 --- /dev/null +++ b/mustslice/must_slice_test.go @@ -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) + }) +} diff --git a/slice_must/slice_must_test.go b/slice_must/slice_must_test.go deleted file mode 100644 index db4ea04..0000000 --- a/slice_must/slice_must_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package slice_must_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/yyle88/must/internal/tests" - "github.com/yyle88/must/slice_must" -) - -func TestEquals(t *testing.T) { - slice_must.Equals([]int{1, 2, 3}, []int{1, 2, 3}) - - tests.ExpectPanic(t, func() { - slice_must.Equals([]string{"a"}, []string{"b"}) - }) -} - -func TestContains(t *testing.T) { - slice_must.Contains([]string{"a", "b", "c"}, "a") - - tests.ExpectPanic(t, func() { - slice_must.Contains([]int{1, 2, 3}, 4) - }) -} - -func TestIn(t *testing.T) { - slice_must.In("a", []string{"a", "b", "c"}) - - tests.ExpectPanic(t, func() { - slice_must.In(4, []int{1, 2, 3}) - }) -} - -func TestNice(t *testing.T) { - require.Equal(t, []int{1, 2, 3}, slice_must.Nice([]int{1, 2, 3})) - - tests.ExpectPanic(t, func() { - slice_must.Nice([]string{}) - }) -}