any_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func Test_get_from_map(t *testing.T) {
  7. any := Any{val: map[string]interface{}{
  8. "hello": "world",
  9. }}
  10. if any.ToString("hello") != "world" {
  11. t.FailNow()
  12. }
  13. }
  14. func Test_get_from_array(t *testing.T) {
  15. any := Any{val: []interface{}{
  16. "hello", "world",
  17. }}
  18. if any.ToString(1) != "world" {
  19. t.FailNow()
  20. }
  21. }
  22. func Test_get_int(t *testing.T) {
  23. any := Any{val: []interface{}{
  24. 1, 2, 3,
  25. }}
  26. if any.ToInt(1) != 2 {
  27. t.FailNow()
  28. }
  29. }
  30. func Test_is_null(t *testing.T) {
  31. any := Any{val: []interface{}{
  32. 1, 2, 3,
  33. }}
  34. if any.IsNil() != false {
  35. t.FailNow()
  36. }
  37. }
  38. func Test_get_bool(t *testing.T) {
  39. any := Any{val: []interface{}{
  40. true, true, false,
  41. }}
  42. if any.ToBool(1) != true {
  43. t.FailNow()
  44. }
  45. }
  46. func Test_nested_read(t *testing.T) {
  47. any := Any{val: []interface{}{
  48. true, map[string]interface{}{
  49. "hello": "world",
  50. }, false,
  51. }}
  52. if any.ToString(1, "hello") != "world" {
  53. fmt.Println(any.Error)
  54. t.FailNow()
  55. }
  56. }
  57. func Test_int_to_string(t *testing.T) {
  58. any := Any{val: []interface{}{
  59. true, 5, false,
  60. }}
  61. if any.ToString(1) != "5" {
  62. t.FailNow()
  63. }
  64. }