|
| 1 | +// Copyright 2024 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/blinklabs-io/plutigo/pkg/data" |
| 22 | +) |
| 23 | + |
| 24 | +// Ttests the ToPlutusData method for Voter types |
| 25 | +func TestVoterToPlutusData(t *testing.T) { |
| 26 | + // Use the same hash value for all tests to avoid confusion |
| 27 | + testHash := [28]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11} |
| 28 | + testHashBytes := []byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11} |
| 29 | + |
| 30 | + testCases := []struct { |
| 31 | + name string |
| 32 | + voter Voter |
| 33 | + expectedData data.PlutusData |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "ConstitutionalCommitteeHotScriptHash", |
| 37 | + voter: Voter{ |
| 38 | + Type: VoterTypeConstitutionalCommitteeHotScriptHash, |
| 39 | + Hash: testHash, |
| 40 | + }, |
| 41 | + expectedData: data.NewConstr(0, data.NewConstr(1, data.NewByteString(testHashBytes))), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "ConstitutionalCommitteeHotKeyHash", |
| 45 | + voter: Voter{ |
| 46 | + Type: VoterTypeConstitutionalCommitteeHotKeyHash, |
| 47 | + Hash: testHash, |
| 48 | + }, |
| 49 | + expectedData: data.NewConstr(0, data.NewConstr(0, data.NewByteString(testHashBytes))), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "DRepScriptHash", |
| 53 | + voter: Voter{ |
| 54 | + Type: VoterTypeDRepScriptHash, |
| 55 | + Hash: testHash, |
| 56 | + }, |
| 57 | + expectedData: data.NewConstr(1, data.NewConstr(1, data.NewByteString(testHashBytes))), |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "DRepKeyHash", |
| 61 | + voter: Voter{ |
| 62 | + Type: VoterTypeDRepKeyHash, |
| 63 | + Hash: testHash, |
| 64 | + }, |
| 65 | + expectedData: data.NewConstr(1, data.NewConstr(0, data.NewByteString(testHashBytes))), |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "StakingPoolKeyHash", |
| 69 | + voter: Voter{ |
| 70 | + Type: VoterTypeStakingPoolKeyHash, |
| 71 | + Hash: testHash, |
| 72 | + }, |
| 73 | + expectedData: data.NewConstr(2, data.NewByteString(testHashBytes)), |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "UnknownType", |
| 77 | + voter: Voter{ |
| 78 | + Type: 255, // Unknown type |
| 79 | + Hash: [28]byte{}, |
| 80 | + }, |
| 81 | + expectedData: nil, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tc := range testCases { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + result := tc.voter.ToPlutusData() |
| 88 | + if !reflect.DeepEqual(result, tc.expectedData) { |
| 89 | + t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Tests the ToPlutusData method for Vote types |
| 96 | +func TestVoteToPlutusData(t *testing.T) { |
| 97 | + testCases := []struct { |
| 98 | + name string |
| 99 | + vote Vote |
| 100 | + expectedData data.PlutusData |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "No", |
| 104 | + vote: Vote(GovVoteNo), |
| 105 | + expectedData: data.NewConstr(0), |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "Yes", |
| 109 | + vote: Vote(GovVoteYes), |
| 110 | + expectedData: data.NewConstr(1), |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Abstain", |
| 114 | + vote: Vote(GovVoteAbstain), |
| 115 | + expectedData: data.NewConstr(2), |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Unknown", |
| 119 | + vote: Vote(255), // Unknown vote |
| 120 | + expectedData: nil, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tc := range testCases { |
| 125 | + t.Run(tc.name, func(t *testing.T) { |
| 126 | + result := tc.vote.ToPlutusData() |
| 127 | + if !reflect.DeepEqual(result, tc.expectedData) { |
| 128 | + t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Tests the ToPlutusData method for VotingProcedure types |
| 135 | +func TestVotingProcedureToPlutusData(t *testing.T) { |
| 136 | + testCases := []struct { |
| 137 | + name string |
| 138 | + procedure VotingProcedure |
| 139 | + expectedData data.PlutusData |
| 140 | + }{ |
| 141 | + { |
| 142 | + name: "NoVote", |
| 143 | + procedure: VotingProcedure{ |
| 144 | + Vote: GovVoteNo, |
| 145 | + }, |
| 146 | + expectedData: data.NewConstr(0), |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "YesVote", |
| 150 | + procedure: VotingProcedure{ |
| 151 | + Vote: GovVoteYes, |
| 152 | + }, |
| 153 | + expectedData: data.NewConstr(1), |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "AbstainVote", |
| 157 | + procedure: VotingProcedure{ |
| 158 | + Vote: GovVoteAbstain, |
| 159 | + }, |
| 160 | + expectedData: data.NewConstr(2), |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "UnknownVote", |
| 164 | + procedure: VotingProcedure{ |
| 165 | + Vote: 255, // Unknown vote |
| 166 | + }, |
| 167 | + expectedData: nil, |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + for _, tc := range testCases { |
| 172 | + t.Run(tc.name, func(t *testing.T) { |
| 173 | + result := tc.procedure.ToPlutusData() |
| 174 | + if !reflect.DeepEqual(result, tc.expectedData) { |
| 175 | + t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments