baselib_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package lua
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. func TestOsDateFormatUTCWithTwoParam(t *testing.T) {
  8. t.Setenv("TZ", "Asia/Tokyo")
  9. ls := NewState()
  10. g := ls.GetGlobal("os")
  11. fn := ls.GetField(g, "date")
  12. int64ptr := func(i int64) *int64 {
  13. return &i
  14. }
  15. cases := []struct {
  16. Name string
  17. Local time.Time
  18. Now time.Time
  19. Format string
  20. Timestamp *int64
  21. }{
  22. {
  23. "UTCWithTwoParam",
  24. time.Now(),
  25. time.Now().UTC(),
  26. "!*t",
  27. int64ptr(time.Now().UTC().Unix()),
  28. },
  29. {
  30. "LocalWithTwoParam",
  31. time.Now(),
  32. time.Now(),
  33. "*t",
  34. int64ptr(time.Now().Unix()),
  35. },
  36. {
  37. "UTCWithOnlyFormatParam",
  38. time.Now(),
  39. time.Now().UTC(),
  40. "!*t",
  41. nil,
  42. },
  43. {
  44. "LocalWithOnlyFormatParam",
  45. time.Now(),
  46. time.Now(),
  47. "*t",
  48. nil,
  49. },
  50. }
  51. for _, c := range cases {
  52. t.Run(c.Name, func(t *testing.T) {
  53. args := make([]LValue, 0)
  54. args = append(args, LString(c.Format))
  55. if c.Timestamp != nil {
  56. args = append(args, LNumber(*c.Timestamp))
  57. }
  58. err := ls.CallByParam(P{
  59. Fn: fn,
  60. NRet: 1,
  61. Protect: true,
  62. }, args...)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. result := ls.ToTable(-1)
  67. resultMap := make(map[string]string)
  68. result.ForEach(func(key LValue, value LValue) {
  69. resultMap[key.String()] = value.String()
  70. assertOsDateFields(t, key, value, c.Now)
  71. })
  72. t.Logf("%v resultMap=%+v\nnow=%+v\nLocal=%+v\nUTC=%v", c.Name, resultMap, c.Now, c.Local, c.Now.UTC())
  73. })
  74. }
  75. }
  76. func TestOsDateFormatLocalWithTwoParam(t *testing.T) {
  77. t.Setenv("TZ", "Asia/Tokyo")
  78. ls := NewState()
  79. g := ls.GetGlobal("os")
  80. fn := ls.GetField(g, "date")
  81. nowLocal := time.Now()
  82. nowUTC := nowLocal.UTC()
  83. err := ls.CallByParam(P{
  84. Fn: fn,
  85. NRet: 1,
  86. Protect: true,
  87. }, LString("*t"), LNumber(nowLocal.Unix()))
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. result := ls.ToTable(-1)
  92. resultMap := make(map[string]string)
  93. result.ForEach(func(key LValue, value LValue) {
  94. t.Logf("key=%v, value=%v", key, value)
  95. resultMap[key.String()] = value.String()
  96. assertOsDateFields(t, key, value, nowLocal)
  97. })
  98. t.Logf("resultMap=%+v, nowLocal=%+v, nowUTC=%v", resultMap, nowLocal, nowUTC)
  99. }
  100. func assertOsDateFields(t *testing.T, key LValue, value LValue, expect time.Time) {
  101. switch key.String() {
  102. case "year":
  103. if value.String() != strconv.Itoa(expect.Year()) {
  104. t.Errorf("year=%v, expect.Year=%v", value.String(), expect.Year())
  105. }
  106. case "month":
  107. if value.String() != strconv.Itoa(int(expect.Month())) {
  108. t.Errorf("month=%v, expect.Month=%v", value.String(), expect.Month())
  109. }
  110. case "day":
  111. if value.String() != strconv.Itoa(expect.Day()) {
  112. t.Errorf("day=%v, expect.Day=%v", value.String(), expect.Day())
  113. }
  114. case "hour":
  115. if value.String() != strconv.Itoa(expect.Hour()) {
  116. t.Errorf("hour=%v, expect.Hour=%v", value.String(), expect.Hour())
  117. }
  118. case "min":
  119. if value.String() != strconv.Itoa(expect.Minute()) {
  120. t.Errorf("min=%v, expect.Minute=%v", value.String(), expect.Minute())
  121. }
  122. case "sec":
  123. if value.String() != strconv.Itoa(expect.Second()) {
  124. t.Errorf("sec=%v, expect.Second=%v", value.String(), expect.Second())
  125. }
  126. }
  127. }