1package monday 2 3import ( 4 // "fmt" 5 "testing" 6) 7 8type layoutData struct { 9 layout string 10 matches []string 11 unmatches []string 12 locales []Locale 13} 14 15var ( 16 testingLayoutsData []layoutData = []layoutData{ 17 layoutData{ 18 layout: "Mon, 2 Jan 2006 15:4:5 -0700", 19 matches: []string{ 20 "Вт, 2 Янв 1997 12:01:44 -0330", 21 "Tue, 12 Feb 2014 22:03:40 -0300", 22 "Fri, 12 Feb 2016 16:01:00 +0000", 23 }, 24 unmatches: []string{ 25 "Mon, 2 Jan 2006 15:4:5 -0700 ", 26 "Mon, 2 Jan 2006 15:4:5 -0700", 27 "Mon, 2 Jan 2006 15:4:5", 28 "Mon, 2 Jan 2006 15:4:5 4 -0700", 29 "Mon, 2 Jan 2006 15:4:5 4 +0200", 30 }, 31 locales: []Locale{ 32 LocaleRuRU, 33 LocaleEnUS, 34 LocaleEnUS, 35 }, 36 }, 37 layoutData{ 38 layout: "2006-01-02T15:04:05-07:00", 39 matches: []string{ 40 "2016-02-02T00:00:00+03:00", 41 "2016-01-26T00:10:21-03:00", 42 "2016-01-26T22:15:30+03:00", 43 }, 44 locales: makeSingleLocalesArray(LocaleEnUS, 3), 45 }, 46 layoutData{ 47 layout: "Пон, 2 Янв 2006 15:4:5 -0700", 48 }, 49 // layoutData{ 50 // layout: "Mon, 2 Jan 2006 15:4:5 -0700", 51 // }, 52 // layoutData{ 53 // layout: "Mon, 2 Jan 2006 15:4:5 -0700", 54 // }, 55 // layoutData{ 56 // layout: "Mon, 2 Jan 2006 15:4:5 -0700", 57 // }, 58 // layoutData{ 59 // layout: "Mon, 2 Jan 2006 15:4:5 -0700", 60 // }, 61 } 62) 63 64/** 65unparsable date: '2016-02-08T00:00:00+03:00' 66unparsable date: '2016-02-02T00:00:00+03:00' 67unparsable date: '2016-02-01T00:00:00+03:00' 68unparsable date: '2016-01-28T00:00:00+03:00' 69unparsable date: '2016-01-26T00:00:00+03:00' 70unparsable date: '2016-01-25T00:00:00+03:00' 71unparsable date: '2016-01-25T00:00:00+03:00' 72unparsable date: '2016-01-22T00:00:00+03:00' 73unparsable date: '2016-01-19T00:00:00+03:00' 74 75unparsable date: 'Fri, 12 Feb 2016 16:01:00 +0000' 76unparsable date: 'Fri, 12 Feb 2016 15:46:00 +0000' 77unparsable date: 'Fri, 12 Feb 2016 15:20:00 +0000' 78unparsable date: 'Fri, 12 Feb 2016 15:06:00 +0000' 79unparsable date: 'Fri, 12 Feb 2016 15:05:00 +0000' 80unparsable date: 'Fri, 12 Feb 2016 15:02:00 +0000' 81 82'Thu, 11 Feb 2016 21:09:52 +0300' 83 84 85**/ 86 87func TestLayoutValidator(t *testing.T) { 88 ld := NewLocaleDetector() 89 for _, ltd := range testingLayoutsData { 90 ld.prepareLayout(ltd.layout) 91 for i, m := range ltd.matches { 92 if !ld.validateValue(ltd.layout, m) { 93 t.Errorf("'%s' not matches to '%s' last error position = %d\n", m, ltd.layout, ld.errorPosition()) 94 } else { 95 t.Logf("'%s' matches to '%s'..OK\n", m, ltd.layout) 96 } 97 var locale Locale = ld.detectLocale(m) 98 if !compareLocales(locale, ltd.locales[i]) { 99 t.Errorf("locales detect error, expected '%s', result '%s'\n", ltd.locales[i], locale) 100 } else { 101 t.Logf("detect locale for '%s': expected '%s', result '%s'\n", m, ltd.locales[i], locale) 102 } 103 } 104 for _, u := range ltd.unmatches { 105 if ld.validateValue(ltd.layout, u) { 106 t.Errorf("'%s' matches to '%s'\n", u, ltd.layout) 107 } else { 108 t.Logf("'%s' not matches to '%s'..OK\n", u, ltd.layout) 109 } 110 } 111 } 112} 113 114// TODO: locale groups. 115var englishLocales = newSet(LocaleEnUS, LocaleEnGB) 116 117func compareLocales(a, b Locale) bool { 118 if a == b { 119 return true 120 } 121 if englishLocales.Has(a) && englishLocales.Has(b) { 122 return true 123 } 124 return false 125} 126 127func TestCompareLocales(t *testing.T) { 128 if !compareLocales(LocaleEnGB, LocaleEnUS) { 129 t.Errorf("compareLocales not works as expected") 130 } 131} 132 133func TestParsing(t *testing.T) { 134 ld := NewLocaleDetector() 135 for _, ltd := range testingLayoutsData { 136 for i, formatted := range ltd.matches { 137 dt, err := ld.Parse(ltd.layout, formatted) 138 if err != nil { 139 t.Errorf("error parsing '%s' with layout: '%s' [error:%s]\n", formatted, ltd.layout, err.Error()) 140 } else { 141 restored := Format(dt, ltd.layout, ltd.locales[i]) 142 if restored != formatted { 143 dt2, err2 := ld.Parse(ltd.layout, restored) 144 if err2 != nil { 145 t.Errorf("parsed time '%s' (%s) does not match restored time '%s'\n", formatted, dt, restored) 146 } 147 if dt2.Unix() != dt.Unix() { 148 t.Errorf("restored time '%v' != parsed time '%v' (restored='%s')", dt2, dt, restored) 149 } 150 } 151 } 152 } 153 } 154} 155 156func makeSingleLocalesArray(loc Locale, length int) []Locale { 157 result := make([]Locale, length) 158 for i := range result { 159 result[i] = loc 160 } 161 return result 162} 163