|
15 | 15 | package ygen |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "strings" |
18 | 19 | "testing" |
19 | 20 |
|
20 | 21 | "github.com/google/go-cmp/cmp" |
21 | 22 | "github.com/openconfig/gnmi/errdiff" |
22 | 23 | "github.com/openconfig/goyang/pkg/yang" |
| 24 | + "github.com/openconfig/ygot/genutil" |
| 25 | + "github.com/openconfig/ygot/yangschema" |
23 | 26 | ) |
24 | 27 |
|
25 | 28 | // errToString returns the string representation of err and the empty string if |
@@ -702,3 +705,176 @@ func TestFindMapPaths(t *testing.T) { |
702 | 705 | }) |
703 | 706 | } |
704 | 707 | } |
| 708 | + |
| 709 | +type mockLangMapper struct{} |
| 710 | + |
| 711 | +func (*mockLangMapper) FieldName(_ *yang.Entry) (string, error) { return "", nil } |
| 712 | + |
| 713 | +func (*mockLangMapper) DirectoryName(_ *yang.Entry, _ genutil.CompressBehaviour) (string, error) { |
| 714 | + return "", nil |
| 715 | +} |
| 716 | + |
| 717 | +func (*mockLangMapper) KeyLeafType(_ *yang.Entry, _ IROptions) (*MappedType, error) { |
| 718 | + return nil, nil |
| 719 | +} |
| 720 | + |
| 721 | +func (*mockLangMapper) LeafType(_ *yang.Entry, _ IROptions) (*MappedType, error) { return nil, nil } |
| 722 | + |
| 723 | +func (*mockLangMapper) PackageName(_ *yang.Entry, _ genutil.CompressBehaviour, _ bool) (string, error) { |
| 724 | + return "", nil |
| 725 | +} |
| 726 | + |
| 727 | +func (*mockLangMapper) InjectEnumSet(_ map[string]*yang.Entry, _, _, _, _, _, _ bool, _ []string) error { |
| 728 | + return nil |
| 729 | +} |
| 730 | + |
| 731 | +func (*mockLangMapper) InjectSchemaTree(_ []*yang.Entry) error { return nil } |
| 732 | + |
| 733 | +func (*mockLangMapper) PopulateEnumFlags(EnumeratedYANGType, *yang.YangType) map[string]string { |
| 734 | + return nil |
| 735 | +} |
| 736 | + |
| 737 | +func (*mockLangMapper) PopulateFieldFlags(NodeDetails, *yang.Entry) map[string]string { return nil } |
| 738 | + |
| 739 | +func (*mockLangMapper) setEnumSet(*enumSet) {} |
| 740 | + |
| 741 | +func (*mockLangMapper) setSchemaTree(*yangschema.Tree) {} |
| 742 | + |
| 743 | +func TestGetOrderedDirDetailsPathOrigin(t *testing.T) { |
| 744 | + ms := compileModules(t, map[string]string{ |
| 745 | + "a-module": ` |
| 746 | + module a-module { |
| 747 | + prefix "m"; |
| 748 | + namespace "urn:m"; |
| 749 | +
|
| 750 | + container a-container { |
| 751 | + leaf field-a { |
| 752 | + type string; |
| 753 | + } |
| 754 | + } |
| 755 | +
|
| 756 | + container b-container { |
| 757 | + container config { |
| 758 | + leaf field-b { |
| 759 | + type string; |
| 760 | + } |
| 761 | + } |
| 762 | + container state { |
| 763 | + leaf field-b { |
| 764 | + type string; |
| 765 | + } |
| 766 | + } |
| 767 | +
|
| 768 | + container c-container { |
| 769 | + leaf field-d { |
| 770 | + type string; |
| 771 | + } |
| 772 | + } |
| 773 | + } |
| 774 | + } |
| 775 | + `, |
| 776 | + }) |
| 777 | + |
| 778 | + tests := []struct { |
| 779 | + name string |
| 780 | + inDirectory map[string]*Directory |
| 781 | + inSchemaTree *yangschema.Tree |
| 782 | + inOpts IROptions |
| 783 | + wantPathOriginName map[string]string |
| 784 | + wantErr bool |
| 785 | + wantErrorSubstrings []string |
| 786 | + }{{ |
| 787 | + name: "PathOriginName is set", |
| 788 | + inDirectory: map[string]*Directory{ |
| 789 | + "/a-module/a-container": { |
| 790 | + Name: "AContainer", |
| 791 | + Entry: findEntry(t, ms, "a-module", "a-container/field-a"), |
| 792 | + Fields: map[string]*yang.Entry{ |
| 793 | + "field-a": findEntry(t, ms, "a-module", "a-container/field-a"), |
| 794 | + }, |
| 795 | + Path: []string{"", "a-module", "a-container"}, |
| 796 | + }, |
| 797 | + }, |
| 798 | + inSchemaTree: &yangschema.Tree{}, |
| 799 | + inOpts: IROptions{ |
| 800 | + PathOriginName: "explicit-origin", |
| 801 | + }, |
| 802 | + wantPathOriginName: map[string]string{ |
| 803 | + "/a-module/a-container/field-a": "explicit-origin", |
| 804 | + }, |
| 805 | + }, { |
| 806 | + name: "UseModuleNameAsPathOrigin is true", |
| 807 | + inDirectory: map[string]*Directory{ |
| 808 | + "/a-module/a-container": { |
| 809 | + Name: "AContainer", |
| 810 | + Entry: findEntry(t, ms, "a-module", "a-container/field-a"), |
| 811 | + Fields: map[string]*yang.Entry{ |
| 812 | + "field-a": findEntry(t, ms, "a-module", "a-container/field-a"), |
| 813 | + }, |
| 814 | + Path: []string{"", "a-module", "a-container"}, |
| 815 | + }, |
| 816 | + }, |
| 817 | + inSchemaTree: &yangschema.Tree{}, |
| 818 | + inOpts: IROptions{ |
| 819 | + UseModuleNameAsPathOrigin: true, |
| 820 | + }, |
| 821 | + wantPathOriginName: map[string]string{ |
| 822 | + "/a-module/a-container/field-a": "a-module", |
| 823 | + }, |
| 824 | + }, { |
| 825 | + name: "PathOriginName and UseModuleNameAsPathOrigin are not set", |
| 826 | + inDirectory: map[string]*Directory{ |
| 827 | + "/a-module/a-container": { |
| 828 | + Name: "AContainer", |
| 829 | + Entry: findEntry(t, ms, "a-module", "a-container/field-a"), |
| 830 | + Fields: map[string]*yang.Entry{ |
| 831 | + "field-a": findEntry(t, ms, "a-module", "a-container/field-a"), |
| 832 | + }, |
| 833 | + Path: []string{"", "a-module", "a-container"}, |
| 834 | + }, |
| 835 | + }, |
| 836 | + inSchemaTree: &yangschema.Tree{}, |
| 837 | + inOpts: IROptions{}, |
| 838 | + wantPathOriginName: map[string]string{ |
| 839 | + "/a-module/a-container/field-a": "", |
| 840 | + }, |
| 841 | + }} |
| 842 | + |
| 843 | + for _, tt := range tests { |
| 844 | + t.Run(tt.name, func(t *testing.T) { |
| 845 | + langMapper := &mockLangMapper{} |
| 846 | + got, err := getOrderedDirDetails(langMapper, tt.inDirectory, tt.inSchemaTree, tt.inOpts) |
| 847 | + |
| 848 | + if tt.wantErr { |
| 849 | + if err == nil { |
| 850 | + t.Fatalf("getOrderedDirDetails() got no error, want error containing: %v", tt.wantErrorSubstrings) |
| 851 | + } |
| 852 | + for _, want := range tt.wantErrorSubstrings { |
| 853 | + if !strings.Contains(err.Error(), want) { |
| 854 | + t.Errorf("getOrderedDirDetails() got error: %v, want error containing: %q", err, want) |
| 855 | + } |
| 856 | + } |
| 857 | + return |
| 858 | + } |
| 859 | + |
| 860 | + if err != nil { |
| 861 | + t.Fatalf("getOrderedDirDetails() unexpected error: %v", err) |
| 862 | + } |
| 863 | + |
| 864 | + if diff := cmp.Diff(tt.wantPathOriginName, getPathOriginNames(got)); diff != "" { |
| 865 | + t.Errorf("getOrderedDirDetails() PathOriginName mismatch (-want +got):\n%s", diff) |
| 866 | + } |
| 867 | + }) |
| 868 | + } |
| 869 | +} |
| 870 | + |
| 871 | +func getPathOriginNames(dirs map[string]*ParsedDirectory) map[string]string { |
| 872 | + origins := make(map[string]string) |
| 873 | + for path, dir := range dirs { |
| 874 | + for _, field := range dir.Fields { |
| 875 | + origins[path] = field.YANGDetails.Origin |
| 876 | + break // The PathOriginName of the first field in each ParsedDirectory is verified. |
| 877 | + } |
| 878 | + } |
| 879 | + return origins |
| 880 | +} |
0 commit comments