launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/names/relation_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package names_test 5 6 import ( 7 gc "launchpad.net/gocheck" 8 9 "launchpad.net/juju-core/names" 10 ) 11 12 type relationSuite struct{} 13 14 var _ = gc.Suite(&relationSuite{}) 15 16 var relationNameTests = []struct { 17 pattern string 18 valid bool 19 }{ 20 {pattern: "", valid: false}, 21 {pattern: "0foo", valid: false}, 22 {pattern: "foo", valid: true}, 23 {pattern: "f1-boo", valid: true}, 24 {pattern: "f-o-o", valid: true}, 25 {pattern: "-foo", valid: false}, 26 {pattern: "fo#o", valid: false}, 27 {pattern: "foo-42", valid: true}, 28 {pattern: "FooBar", valid: false}, 29 {pattern: "foo42-bar1", valid: true}, 30 {pattern: "42", valid: false}, 31 {pattern: "0", valid: false}, 32 {pattern: "%not", valid: false}, 33 {pattern: "42also-not", valid: false}, 34 {pattern: "042", valid: false}, 35 {pattern: "0x42", valid: false}, 36 {pattern: "foo_42", valid: true}, 37 {pattern: "_foo", valid: false}, 38 {pattern: "!foo", valid: false}, 39 {pattern: "foo_bar-baz_boo", valid: true}, 40 {pattern: "foo bar", valid: false}, 41 {pattern: "foo-_", valid: false}, 42 {pattern: "foo-", valid: false}, 43 {pattern: "foo_-a", valid: false}, 44 {pattern: "foo_", valid: false}, 45 } 46 47 func (s *relationSuite) TestRelationKeyFormats(c *gc.C) { 48 // In order to test all possible combinations, we need 49 // to use the relationNameTests and serviceNameTests 50 // twice to construct all possible keys. 51 for i, testRel := range relationNameTests { 52 for j, testSvc := range serviceNameTests { 53 peerKey := testSvc.pattern + ":" + testRel.pattern 54 key := peerKey + " " + peerKey 55 isValid := testSvc.valid && testRel.valid 56 c.Logf("test %d: %q -> valid: %v", i*len(serviceNameTests)+j, key, isValid) 57 c.Assert(names.IsRelation(key), gc.Equals, isValid) 58 c.Assert(names.IsRelation(peerKey), gc.Equals, isValid) 59 } 60 } 61 }