|
1 | 1 | package semver
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
4 | 7 | "reflect"
|
5 | 8 | "testing"
|
6 | 9 | )
|
@@ -676,3 +679,56 @@ func TestConstraintString(t *testing.T) {
|
676 | 679 | }
|
677 | 680 | }
|
678 | 681 | }
|
| 682 | + |
| 683 | +func TestJsonMarshalConstraints(t *testing.T) { |
| 684 | + tests := []struct { |
| 685 | + sCs string |
| 686 | + want string |
| 687 | + }{ |
| 688 | + {"1.1.1", "1.1.1"}, |
| 689 | + {">=1.1.1", ">=1.1.1"}, |
| 690 | + {"<=1.1.1", "<=1.1.1"}, |
| 691 | + } |
| 692 | + |
| 693 | + for _, tc := range tests { |
| 694 | + cs, err := NewConstraint(tc.sCs) |
| 695 | + if err != nil { |
| 696 | + t.Errorf("Error creating constraints: %s", err) |
| 697 | + } |
| 698 | + buf := new(bytes.Buffer) |
| 699 | + enc := json.NewEncoder(buf) |
| 700 | + enc.SetEscapeHTML(false) |
| 701 | + err = enc.Encode(cs) |
| 702 | + if err != nil { |
| 703 | + t.Errorf("Error unmarshaling version: %s", err) |
| 704 | + } |
| 705 | + got := buf.String() |
| 706 | + want := fmt.Sprintf("%q\n", tc.want) |
| 707 | + if got != want { |
| 708 | + t.Errorf("Error marshaling unexpected marshaled content: got=%q want=%q", got, want) |
| 709 | + } |
| 710 | + } |
| 711 | +} |
| 712 | + |
| 713 | +func TestJsonUnmarshalConstraints(t *testing.T) { |
| 714 | + tests := []struct { |
| 715 | + sCs string |
| 716 | + want string |
| 717 | + }{ |
| 718 | + {"1.1.1", "1.1.1"}, |
| 719 | + {">=1.2.3", ">=1.2.3"}, |
| 720 | + {"<=1.2.3", "<=1.2.3"}, |
| 721 | + } |
| 722 | + |
| 723 | + for _, tc := range tests { |
| 724 | + cs := Constraints{} |
| 725 | + err := json.Unmarshal([]byte(fmt.Sprintf("%q", tc.sCs)), &cs) |
| 726 | + if err != nil { |
| 727 | + t.Errorf("Error unmarshaling constraints: %s", err) |
| 728 | + } |
| 729 | + got := cs.String() |
| 730 | + if got != tc.want { |
| 731 | + t.Errorf("Error unmarshaling unexpected object content: got=%q want=%q", got, tc.want) |
| 732 | + } |
| 733 | + } |
| 734 | +} |
0 commit comments