Skip to content

Commit

Permalink
把包名map_must和slice_must改为mustmap和mustslice以保持符合规范和语境
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Nov 28, 2024
1 parent 8bda865 commit fea23ee
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 130 deletions.
45 changes: 0 additions & 45 deletions map_must/map_must.go

This file was deleted.

35 changes: 0 additions & 35 deletions map_must/map_must_test.go

This file was deleted.

59 changes: 59 additions & 0 deletions mustmap/must_map.go
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
}
105 changes: 105 additions & 0 deletions mustmap/must_map_test.go
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")
})
}
24 changes: 15 additions & 9 deletions slice_must/slice_must.go → mustslice/must_slice.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slice_must
package mustslice

import (
"slices"
Expand All @@ -7,51 +7,57 @@ 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")
}
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))
Expand Down
79 changes: 79 additions & 0 deletions mustslice/must_slice_test.go
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)
})
}
Loading

0 comments on commit fea23ee

Please sign in to comment.