| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build goexperiment.jsonv2 |
| 6 |
|
| 7 |
package jsonflags |
| 8 |
|
| 9 |
import "testing" |
| 10 |
|
| 11 |
func TestFlags(t *testing.T) { |
| 12 |
type Check struct{ want Flags } |
| 13 |
type Join struct{ in Flags } |
| 14 |
type Set struct{ in Bools } |
| 15 |
type Clear struct{ in Bools } |
| 16 |
type Get struct { |
| 17 |
in Bools |
| 18 |
want bool |
| 19 |
wantOk bool |
| 20 |
} |
| 21 |
|
| 22 |
calls := []any{ |
| 23 |
Get{in: AllowDuplicateNames, want: false, wantOk: false}, |
| 24 |
Set{in: AllowDuplicateNames | 0}, |
| 25 |
Get{in: AllowDuplicateNames, want: false, wantOk: true}, |
| 26 |
Set{in: AllowDuplicateNames | 1}, |
| 27 |
Get{in: AllowDuplicateNames, want: true, wantOk: true}, |
| 28 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames), Values: uint64(AllowDuplicateNames)}}, |
| 29 |
Get{in: AllowInvalidUTF8, want: false, wantOk: false}, |
| 30 |
Set{in: AllowInvalidUTF8 | 1}, |
| 31 |
Get{in: AllowInvalidUTF8, want: true, wantOk: true}, |
| 32 |
Set{in: AllowInvalidUTF8 | 0}, |
| 33 |
Get{in: AllowInvalidUTF8, want: false, wantOk: true}, |
| 34 |
Get{in: AllowDuplicateNames, want: true, wantOk: true}, |
| 35 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(AllowDuplicateNames)}}, |
| 36 |
Set{in: AllowDuplicateNames | AllowInvalidUTF8 | 0}, |
| 37 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(0)}}, |
| 38 |
Set{in: AllowDuplicateNames | AllowInvalidUTF8 | 0}, |
| 39 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(0)}}, |
| 40 |
Set{in: AllowDuplicateNames | AllowInvalidUTF8 | 1}, |
| 41 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(AllowDuplicateNames | AllowInvalidUTF8)}}, |
| 42 |
Join{in: Flags{Presence: 0, Values: 0}}, |
| 43 |
Check{want: Flags{Presence: uint64(AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(AllowDuplicateNames | AllowInvalidUTF8)}}, |
| 44 |
Join{in: Flags{Presence: uint64(Multiline | AllowInvalidUTF8), Values: uint64(AllowDuplicateNames)}}, |
| 45 |
Check{want: Flags{Presence: uint64(Multiline | AllowDuplicateNames | AllowInvalidUTF8), Values: uint64(AllowDuplicateNames)}}, |
| 46 |
Clear{in: AllowDuplicateNames | AllowInvalidUTF8}, |
| 47 |
Check{want: Flags{Presence: uint64(Multiline), Values: uint64(0)}}, |
| 48 |
Set{in: AllowInvalidUTF8 | Deterministic | ReportErrorsWithLegacySemantics | 1}, |
| 49 |
Set{in: Multiline | StringifyNumbers | 0}, |
| 50 |
Check{want: Flags{Presence: uint64(AllowInvalidUTF8 | Deterministic | ReportErrorsWithLegacySemantics | Multiline | StringifyNumbers), Values: uint64(AllowInvalidUTF8 | Deterministic | ReportErrorsWithLegacySemantics)}}, |
| 51 |
Clear{in: ^AllCoderFlags}, |
| 52 |
Check{want: Flags{Presence: uint64(AllowInvalidUTF8 | Multiline), Values: uint64(AllowInvalidUTF8)}}, |
| 53 |
} |
| 54 |
var fs Flags |
| 55 |
for i, call := range calls { |
| 56 |
switch call := call.(type) { |
| 57 |
case Join: |
| 58 |
fs.Join(call.in) |
| 59 |
case Set: |
| 60 |
fs.Set(call.in) |
| 61 |
case Clear: |
| 62 |
fs.Clear(call.in) |
| 63 |
case Get: |
| 64 |
got := fs.Get(call.in) |
| 65 |
gotOk := fs.Has(call.in) |
| 66 |
if got != call.want || gotOk != call.wantOk { |
| 67 |
t.Fatalf("%d: GetOk = (%v, %v), want (%v, %v)", i, got, gotOk, call.want, call.wantOk) |
| 68 |
} |
| 69 |
case Check: |
| 70 |
if fs != call.want { |
| 71 |
t.Fatalf("%d: got %x, want %x", i, fs, call.want) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |