platform_flag_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package main
  2. import (
  3. "flag"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestPlatformFlagPlatforms(t *testing.T) {
  8. cases := []struct {
  9. OS []string
  10. Arch []string
  11. OSArch []Platform
  12. Supported []Platform
  13. Result []Platform
  14. }{
  15. // Building a new list of platforms
  16. {
  17. []string{"foo", "bar"},
  18. []string{"baz"},
  19. []Platform{},
  20. []Platform{
  21. {"foo", "baz", true},
  22. {"bar", "baz", true},
  23. {"boo", "bop", true},
  24. },
  25. []Platform{
  26. {"foo", "baz", false},
  27. {"bar", "baz", false},
  28. },
  29. },
  30. // Skipping platforms
  31. {
  32. []string{"!foo"},
  33. []string{},
  34. []Platform{},
  35. []Platform{
  36. {"foo", "bar", true},
  37. {"foo", "baz", true},
  38. {"bar", "bar", true},
  39. },
  40. []Platform{
  41. {"bar", "bar", false},
  42. },
  43. },
  44. // Specifying only an OS
  45. {
  46. []string{"foo"},
  47. []string{},
  48. []Platform{},
  49. []Platform{
  50. {"foo", "bar", true},
  51. {"foo", "baz", true},
  52. {"bar", "bar", true},
  53. },
  54. []Platform{
  55. {"foo", "bar", false},
  56. {"foo", "baz", false},
  57. },
  58. },
  59. // Building a new list, but with some skips
  60. {
  61. []string{"foo", "bar", "!foo"},
  62. []string{"baz"},
  63. []Platform{},
  64. []Platform{
  65. {"foo", "bar", true},
  66. {"foo", "baz", true},
  67. {"bar", "baz", true},
  68. {"baz", "bar", true},
  69. },
  70. []Platform{
  71. {"bar", "baz", false},
  72. },
  73. },
  74. // Unsupported pairs
  75. {
  76. []string{"foo", "bar"},
  77. []string{"baz"},
  78. []Platform{},
  79. []Platform{
  80. {"foo", "baz", true},
  81. {"bar", "what", true},
  82. },
  83. []Platform{
  84. {"foo", "baz", false},
  85. },
  86. },
  87. // OSArch basic
  88. {
  89. []string{},
  90. []string{},
  91. []Platform{
  92. {"foo", "baz", true},
  93. {"foo", "bar", true},
  94. },
  95. []Platform{
  96. {"foo", "baz", true},
  97. {"bar", "what", true},
  98. },
  99. []Platform{
  100. {"foo", "baz", false},
  101. },
  102. },
  103. // Negative OSArch
  104. {
  105. []string{},
  106. []string{},
  107. []Platform{
  108. {"!foo", "baz", true},
  109. },
  110. []Platform{
  111. {"foo", "baz", true},
  112. {"bar", "what", true},
  113. },
  114. []Platform{
  115. {"bar", "what", false},
  116. },
  117. },
  118. // Mix it all
  119. {
  120. []string{"foo", "bar"},
  121. []string{"bar"},
  122. []Platform{
  123. {"foo", "baz", true},
  124. {"!bar", "bar", true},
  125. },
  126. []Platform{
  127. {"foo", "bar", true},
  128. {"foo", "baz", true},
  129. {"bar", "bar", true},
  130. },
  131. []Platform{
  132. {"foo", "baz", false},
  133. {"foo", "bar", false},
  134. },
  135. },
  136. // Ignores non-default
  137. {
  138. []string{},
  139. []string{},
  140. []Platform{},
  141. []Platform{
  142. {"foo", "bar", true},
  143. {"foo", "baz", true},
  144. {"bar", "bar", false},
  145. },
  146. []Platform{
  147. {"foo", "bar", false},
  148. {"foo", "baz", false},
  149. },
  150. },
  151. // Adds non-default by OS
  152. {
  153. []string{"bar"},
  154. []string{},
  155. []Platform{},
  156. []Platform{
  157. {"foo", "bar", true},
  158. {"foo", "baz", true},
  159. {"bar", "bar", false},
  160. },
  161. []Platform{
  162. {"bar", "bar", false},
  163. },
  164. },
  165. // Adds non-default by both
  166. {
  167. []string{"bar"},
  168. []string{"bar"},
  169. []Platform{},
  170. []Platform{
  171. {"foo", "bar", true},
  172. {"foo", "baz", true},
  173. {"bar", "bar", false},
  174. },
  175. []Platform{
  176. {"bar", "bar", false},
  177. },
  178. },
  179. }
  180. for _, tc := range cases {
  181. f := PlatformFlag{
  182. OS: tc.OS,
  183. Arch: tc.Arch,
  184. OSArch: tc.OSArch,
  185. }
  186. result := f.Platforms(tc.Supported)
  187. if !reflect.DeepEqual(result, tc.Result) {
  188. t.Errorf("input: %#v\nresult: %#v", f, result)
  189. }
  190. }
  191. }
  192. func TestPlatformFlagArchFlagValue(t *testing.T) {
  193. var f PlatformFlag
  194. val := f.ArchFlagValue()
  195. if err := val.Set("foo bar"); err != nil {
  196. t.Fatalf("err: %s", err)
  197. }
  198. expected := []string{"foo", "bar"}
  199. if !reflect.DeepEqual(f.Arch, expected) {
  200. t.Fatalf("bad: %#v", f.Arch)
  201. }
  202. }
  203. func TestPlatformFlagOSArchFlagValue(t *testing.T) {
  204. var f PlatformFlag
  205. val := f.OSArchFlagValue()
  206. if err := val.Set("foo/bar"); err != nil {
  207. t.Fatalf("err: %s", err)
  208. }
  209. expected := []Platform{{"foo", "bar", false}}
  210. if !reflect.DeepEqual(f.OSArch, expected) {
  211. t.Fatalf("bad: %#v", f.OSArch)
  212. }
  213. }
  214. func TestPlatformFlagOSFlagValue(t *testing.T) {
  215. var f PlatformFlag
  216. val := f.OSFlagValue()
  217. if err := val.Set("foo bar"); err != nil {
  218. t.Fatalf("err: %s", err)
  219. }
  220. expected := []string{"foo", "bar"}
  221. if !reflect.DeepEqual(f.OS, expected) {
  222. t.Fatalf("bad: %#v", f.OS)
  223. }
  224. }
  225. func TestAppendPlatformValue_impl(t *testing.T) {
  226. var _ flag.Value = new(appendPlatformValue)
  227. }
  228. func TestAppendPlatformValue(t *testing.T) {
  229. var value appendPlatformValue
  230. if err := value.Set(""); err != nil {
  231. t.Fatalf("err: %s", err)
  232. }
  233. if len(value) > 0 {
  234. t.Fatalf("bad: %#v", value)
  235. }
  236. if err := value.Set("windows/arm/bad"); err == nil {
  237. t.Fatal("should err")
  238. }
  239. if err := value.Set("windows"); err == nil {
  240. t.Fatal("should err")
  241. }
  242. if err := value.Set("windows/arm windows/386"); err != nil {
  243. t.Fatalf("err: %s", err)
  244. }
  245. expected := []Platform{
  246. {"windows", "arm", false},
  247. {"windows", "386", false},
  248. }
  249. if !reflect.DeepEqual([]Platform(value), expected) {
  250. t.Fatalf("bad: %#v", value)
  251. }
  252. }
  253. func TestAppendStringValue_impl(t *testing.T) {
  254. var _ flag.Value = new(appendStringValue)
  255. }
  256. func TestAppendStringValue(t *testing.T) {
  257. var value appendStringValue
  258. if err := value.Set(""); err != nil {
  259. t.Fatalf("err: %s", err)
  260. }
  261. if len(value) > 0 {
  262. t.Fatalf("bad: %#v", value)
  263. }
  264. if err := value.Set("windows LINUX"); err != nil {
  265. t.Fatalf("err: %s", err)
  266. }
  267. expected := []string{"windows", "linux"}
  268. if !reflect.DeepEqual([]string(value), expected) {
  269. t.Fatalf("bad: %#v", value)
  270. }
  271. if err := value.Set("darwin"); err != nil {
  272. t.Fatalf("err: %s", err)
  273. }
  274. expected = []string{"windows", "linux", "darwin"}
  275. if !reflect.DeepEqual([]string(value), expected) {
  276. t.Fatalf("bad: %#v", value)
  277. }
  278. if err := value.Set("darwin"); err != nil {
  279. t.Fatalf("err: %s", err)
  280. }
  281. expected = []string{"windows", "linux", "darwin"}
  282. if !reflect.DeepEqual([]string(value), expected) {
  283. t.Fatalf("bad: %#v", value)
  284. }
  285. }