@@ -680,55 +680,91 @@ func TestConstraintString(t *testing.T) {
680
680
}
681
681
}
682
682
683
- func TestJsonMarshalConstraints (t * testing.T ) {
683
+ func TestTextMarshalConstraints (t * testing.T ) {
684
684
tests := []struct {
685
- sCs string
686
- want string
685
+ constraint string
686
+ want string
687
687
}{
688
- {"1.1.1" , "1.1.1" },
689
- {">=1.1.1" , ">=1.1.1" },
690
- {"<=1.1.1" , "<=1.1.1" },
688
+ {"1.2.3" , "1.2.3" },
689
+ {">=1.2.3" , ">=1.2.3" },
690
+ {"<=1.2.3" , "<=1.2.3" },
691
+ {"1 <=1.2.3" , "1 <=1.2.3" },
692
+ {"1, <=1.2.3" , "1 <=1.2.3" },
693
+ {">1, <=1.2.3" , ">1 <=1.2.3" },
694
+ {"> 1 , <=1.2.3" , ">1 <=1.2.3" },
691
695
}
692
696
693
697
for _ , tc := range tests {
694
- cs , err := NewConstraint (tc .sCs )
698
+ cs , err := NewConstraint (tc .constraint )
695
699
if err != nil {
696
700
t .Errorf ("Error creating constraints: %s" , err )
697
701
}
702
+
703
+ out , err2 := cs .MarshalText ()
704
+ if err2 != nil {
705
+ t .Errorf ("Error constraint version: %s" , err2 )
706
+ }
707
+
708
+ got := string (out )
709
+ if got != tc .want {
710
+ t .Errorf ("Error marshaling constraint, unexpected marshaled content: got=%q want=%q" , got , tc .want )
711
+ }
712
+
713
+ // Test that this works for JSON as well as text. When JSON marshaling
714
+ // functions are missing it falls through to TextMarshal.
715
+ // NOTE: To not escape the < and > (which json.Marshal does) you need
716
+ // a custom encoder where html escaping is disabled. This must be done
717
+ // in the top level encoder being used to marshal the constraints.
698
718
buf := new (bytes.Buffer )
699
719
enc := json .NewEncoder (buf )
700
720
enc .SetEscapeHTML (false )
701
721
err = enc .Encode (cs )
702
722
if err != nil {
703
- t .Errorf ("Error unmarshaling version : %s" , err )
723
+ t .Errorf ("Error unmarshaling constraint : %s" , err )
704
724
}
705
- got := buf .String ()
725
+ got = buf .String ()
726
+ // The encoder used here adds a newline so we add that to what we want
727
+ // so they align. The newline is an artifact of the testing.
706
728
want := fmt .Sprintf ("%q\n " , tc .want )
707
729
if got != want {
708
- t .Errorf ("Error marshaling unexpected marshaled content: got=%q want=%q" , got , want )
730
+ t .Errorf ("Error marshaling constraint, unexpected marshaled content: got=%q want=%q" , got , want )
709
731
}
710
732
}
711
733
}
712
734
713
- func TestJsonUnmarshalConstraints (t * testing.T ) {
735
+ func TestTextUnmarshalConstraints (t * testing.T ) {
714
736
tests := []struct {
715
- sCs string
716
- want string
737
+ constraint string
738
+ want string
717
739
}{
718
- {"1.1.1 " , "1.1.1 " },
740
+ {"1.2.3 " , "1.2.3 " },
719
741
{">=1.2.3" , ">=1.2.3" },
720
742
{"<=1.2.3" , "<=1.2.3" },
743
+ {">1 <=1.2.3" , ">1 <=1.2.3" },
744
+ {"> 1 <=1.2.3" , ">1 <=1.2.3" },
745
+ {">1, <=1.2.3" , ">1 <=1.2.3" },
721
746
}
722
747
723
748
for _ , tc := range tests {
724
749
cs := Constraints {}
725
- err := json . Unmarshal ([]byte (fmt . Sprintf ( "%q" , tc .sCs )), & cs )
750
+ err := cs . UnmarshalText ([]byte (tc .constraint ) )
726
751
if err != nil {
727
752
t .Errorf ("Error unmarshaling constraints: %s" , err )
728
753
}
729
754
got := cs .String ()
730
755
if got != tc .want {
731
- t .Errorf ("Error unmarshaling unexpected object content: got=%q want=%q" , got , tc .want )
756
+ t .Errorf ("Error unmarshaling constraint, unexpected object content: got=%q want=%q" , got , tc .want )
757
+ }
758
+
759
+ // Test that this works for JSON as well as text. When JSON unmarshaling
760
+ // functions are missing it falls through to TextUnmarshal.
761
+ err = json .Unmarshal ([]byte (fmt .Sprintf ("%q" , tc .constraint )), & cs )
762
+ if err != nil {
763
+ t .Errorf ("Error unmarshaling constraints: %s" , err )
764
+ }
765
+ got = cs .String ()
766
+ if got != tc .want {
767
+ t .Errorf ("Error unmarshaling constraint, unexpected object content: got=%q want=%q" , got , tc .want )
732
768
}
733
769
}
734
770
}
0 commit comments