github.com/bazelbuild/remote-apis-sdks@v0.0.0-20240425170053-8a36686a6350/go/pkg/contextmd/contextmd_test.go (about) 1 package contextmd 2 3 import ( 4 "testing" 5 ) 6 7 func TestCapToLimit(t *testing.T) { 8 type testCase struct { 9 name string 10 limit int 11 input *Metadata 12 want *Metadata 13 } 14 tests := []testCase{ 15 { 16 name: "under limit", 17 limit: 24, 18 input: &Metadata{ 19 ToolName: "toolName", 20 ActionID: "actionID", 21 InvocationID: "invocID*", 22 }, 23 want: &Metadata{ 24 ToolName: "toolName", 25 ActionID: "actionID", 26 InvocationID: "invocID*", 27 }, 28 }, 29 { 30 name: "actionID over limit", 31 limit: 24, 32 input: &Metadata{ 33 ToolName: "toolName", 34 ActionID: "actionID-12345678", 35 InvocationID: "invocID*", 36 }, 37 want: &Metadata{ 38 ToolName: "toolName", 39 ActionID: "actionID", 40 InvocationID: "invocID*", 41 }, 42 }, 43 { 44 name: "invocationID over limit", 45 limit: 29, 46 input: &Metadata{ 47 ToolName: "toolName", 48 ToolVersion: "1.2.3", 49 ActionID: "actionID", 50 InvocationID: "invocID*-12345678", 51 }, 52 want: &Metadata{ 53 ToolName: "toolName", 54 ToolVersion: "1.2.3", 55 ActionID: "actionID", 56 InvocationID: "invocID*", 57 }, 58 }, 59 { 60 name: "both equally over limit", 61 limit: 24, 62 input: &Metadata{ 63 ToolName: "toolName", 64 ActionID: "actionID-12345678", 65 InvocationID: "invocID*-12345678", 66 }, 67 want: &Metadata{ 68 ToolName: "toolName", 69 ActionID: "actionID", 70 InvocationID: "invocID*", 71 }, 72 }, 73 { 74 name: "both over limit but actionID is bigger", 75 limit: 24, 76 input: &Metadata{ 77 ToolName: "toolName", 78 ActionID: "actionID-123456789012345678", 79 InvocationID: "invocID*-12345678", 80 }, 81 want: &Metadata{ 82 ToolName: "toolName", 83 ActionID: "actionID", 84 InvocationID: "invocID*", 85 }, 86 }, 87 { 88 name: "both over limit but invocationID is bigger", 89 limit: 24, 90 input: &Metadata{ 91 ToolName: "toolName", 92 ActionID: "actionID-12345678", 93 InvocationID: "invocID*-123456789012345678", 94 }, 95 want: &Metadata{ 96 ToolName: "toolName", 97 ActionID: "actionID", 98 InvocationID: "invocID*", 99 }, 100 }, 101 } 102 for _, tc := range tests { 103 t.Run(tc.name, func(t *testing.T) { 104 capToLimit(tc.input, tc.limit) 105 if *tc.input != *tc.want { 106 t.Errorf("Got %+v, want %+v", tc.input, tc.want) 107 } 108 }) 109 } 110 }