You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestIterateKeysWithValues(t *testing.T) {
  9. cmap := NewCMap()
  10. for i := 1; i <= 10; i++ {
  11. cmap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
  12. }
  13. // Testing size
  14. assert.Equal(t, cmap.Size(), 10, "overall size should be 10")
  15. assert.Equal(t, len(cmap.Keys()), 10, "should be 10 keys")
  16. assert.Equal(t, len(cmap.Values()), 10, "should be 10 values")
  17. // Iterating Keys, checking for matching Value
  18. for _, key := range cmap.Keys() {
  19. val := strings.Replace(key, "key", "value", -1)
  20. assert.Equal(t, cmap.Get(key), val)
  21. }
  22. // Test if all keys are within []Keys()
  23. keys := cmap.Keys()
  24. for i := 1; i <= 10; i++ {
  25. assert.True(t, contains(keys, fmt.Sprintf("key%d", i)), "cmap.Keys() should contain key")
  26. }
  27. // Delete 1 Key
  28. cmap.Delete("key1")
  29. assert.NotEqual(t, len(keys), len(cmap.Keys()), "[]keys and []Keys() should not be equal, they are copies, one item was removed")
  30. }
  31. func TestContains(t *testing.T) {
  32. cmap := NewCMap()
  33. cmap.Set("key1", "value1")
  34. // Test for known values
  35. assert.True(t, cmap.Has("key1"), "should contain key1")
  36. assert.Equal(t, cmap.Get("key1"), "value1", "key1.value() should be value1")
  37. // Test for unknown values
  38. assert.False(t, cmap.Has("key2"), "should not contain key2")
  39. assert.Nil(t, cmap.Get("key2"), "does not contain key2")
  40. }
  41. func contains(array []string, value string) (bool) {
  42. for _, val := range array {
  43. if val == value {
  44. return true
  45. }
  46. }
  47. return false
  48. }