go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamproto/streamName_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package streamproto 16 17 import ( 18 "encoding/json" 19 "flag" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 ) 24 25 func TestStreamName(t *testing.T) { 26 Convey(`A StreamNameFlag`, t, func() { 27 sn := StreamNameFlag("") 28 29 Convey(`When attached to a flag.`, func() { 30 fs := flag.FlagSet{} 31 fs.Var(&sn, "name", "Test stream name.") 32 33 Convey(`Will successfully parse a valid stream name.`, func() { 34 err := fs.Parse([]string{"-name", "test/stream/name"}) 35 So(err, ShouldBeNil) 36 So(sn, ShouldEqual, StreamNameFlag("test/stream/name")) 37 }) 38 39 Convey(`Will fail to parse an invalid stream name.`, func() { 40 err := fs.Parse([]string{"-name", "test;stream;name"}) 41 So(err, ShouldNotBeNil) 42 }) 43 }) 44 45 Convey(`With valid value "test/stream/name"`, func() { 46 Convey(`Will marshal into a JSON string.`, func() { 47 sn = "test/stream/name" 48 d, err := json.Marshal(&sn) 49 So(err, ShouldBeNil) 50 So(string(d), ShouldEqual, `"test/stream/name"`) 51 52 Convey(`And successfully unmarshal.`, func() { 53 sn = "" 54 err := json.Unmarshal(d, &sn) 55 So(err, ShouldBeNil) 56 So(sn, ShouldEqual, StreamNameFlag("test/stream/name")) 57 }) 58 }) 59 }) 60 61 Convey(`JSON with an invalid value "test;stream;name" will fail to Unmarshal.`, func() { 62 sn := StreamNameFlag("") 63 err := json.Unmarshal([]byte(`"test;stream;name"`), &sn) 64 So(err, ShouldNotBeNil) 65 }) 66 }) 67 }