Skip to content

Commit 500ab12

Browse files
committed
move tests into separate files
1 parent 2a1873f commit 500ab12

File tree

10 files changed

+1314
-1329
lines changed

10 files changed

+1314
-1329
lines changed

clarity-serialization/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,3 @@ pub fn version_string(pkg_name: &str, pkg_version: &str) -> String {
5656
std::env::consts::ARCH
5757
)
5858
}
59-
60-
#[cfg(test)]
61-
mod lib_tests {
62-
use super::*;
63-
64-
#[test]
65-
fn test_version_string_basic_no_env() {
66-
let version = version_string("test-package", "1.0.0");
67-
68-
assert_eq!(
69-
version,
70-
format!(
71-
"test-package 1.0.0 (:, {BUILD_TYPE} build, {} [{}])",
72-
std::env::consts::OS,
73-
std::env::consts::ARCH
74-
)
75-
);
76-
}
77-
}

clarity-serialization/src/representations.rs

Lines changed: 0 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -160,203 +160,3 @@ impl StacksMessageCodec for ContractName {
160160
Ok(name)
161161
}
162162
}
163-
164-
#[cfg(test)]
165-
mod tests {
166-
use test_case::test_case;
167-
168-
use super::*;
169-
170-
#[test_case("hello"; "valid_name")]
171-
#[test_case("hello-dash"; "dash")]
172-
#[test_case("hello_underscore"; "underscore")]
173-
#[test_case("test123"; "numbers")]
174-
#[test_case("a"; "single_letter")]
175-
#[test_case("set-token-uri!"; "exclamation_mark")]
176-
#[test_case("is-owner?"; "question_mark")]
177-
#[test_case("math+"; "plus")]
178-
#[test_case("greater-than<"; "less_than")]
179-
#[test_case("less-than>"; "greater_than")]
180-
#[test_case("<="; "less_than_or_equal_to")]
181-
#[test_case(">="; "greater_than_or_equal_to")]
182-
#[test_case("*"; "asterisk")]
183-
#[test_case("/"; "slash")]
184-
#[test_case("-"; "dash-only")]
185-
#[test_case("="; "equals")]
186-
fn test_clarity_name_valid(name: &str) {
187-
let clarity_name = ClarityName::try_from(name.to_string())
188-
.unwrap_or_else(|_| panic!("Should parse valid clarity name: {name}"));
189-
assert_eq!(clarity_name.as_str(), name);
190-
}
191-
192-
#[test_case(""; "empty")]
193-
#[test_case("123abc"; "starts_with_number")]
194-
#[test_case("hello world"; "contains_space")]
195-
#[test_case("hello@world"; "contains_at")]
196-
#[test_case("hello#world"; "contains_hash")]
197-
#[test_case("hello$world"; "contains_dollar")]
198-
#[test_case("hello%world"; "contains_percent")]
199-
#[test_case("hello&world"; "contains_ampersand")]
200-
#[test_case("hello.world"; "contains_dot")]
201-
#[test_case("hello,world"; "contains_comma")]
202-
#[test_case("hello;world"; "contains_semicolon")]
203-
#[test_case("hello:world"; "contains_colon")]
204-
#[test_case("hello|world"; "contains_pipe")]
205-
#[test_case("hello\\world"; "contains_backslash")]
206-
#[test_case("hello\"world"; "contains_quote")]
207-
#[test_case("hello'world"; "contains_apostrophe")]
208-
#[test_case("hello[world"; "contains_bracket_open")]
209-
#[test_case("hello]world"; "contains_bracket_close")]
210-
#[test_case("hello{world"; "contains_curly_open")]
211-
#[test_case("hello}world"; "contains_curly_close")]
212-
#[test_case("hello(world"; "contains_parenthesis_open")]
213-
#[test_case("hello)world"; "contains_parenthesis_close")]
214-
#[test_case(&"a".repeat(MAX_STRING_LEN as usize + 1); "too_long")]
215-
fn test_clarity_name_invalid(name: &str) {
216-
let result = ClarityName::try_from(name.to_string());
217-
assert!(result.is_err());
218-
assert!(matches!(
219-
result.unwrap_err(),
220-
CodecError::InvalidClarityName(_, _)
221-
));
222-
}
223-
224-
#[test_case("test-name")]
225-
#[test_case(&"a".repeat(MAX_STRING_LEN as usize); "max-length")]
226-
fn test_clarity_name_serialization(name: &str) {
227-
let name = ClarityName::try_from(name.to_string()).unwrap();
228-
229-
let mut buffer = Vec::new();
230-
name.consensus_serialize(&mut buffer)
231-
.unwrap_or_else(|_| panic!("Serialization should succeed for name: {name}"));
232-
233-
// Should have length byte followed by the string bytes
234-
assert_eq!(buffer[0], name.len());
235-
assert_eq!(&buffer[1..], name.as_bytes());
236-
237-
// Test deserialization
238-
let deserialized = ClarityName::consensus_deserialize(&mut buffer.as_slice()).unwrap();
239-
assert_eq!(deserialized, name);
240-
}
241-
242-
#[test]
243-
fn test_clarity_name_serialization_too_long() {
244-
let name = ClarityName("a".repeat(MAX_STRING_LEN as usize + 1));
245-
let mut buffer = Vec::new();
246-
let result = name.consensus_serialize(&mut buffer);
247-
assert!(result.is_err());
248-
assert_eq!(
249-
result.unwrap_err().to_string(),
250-
"Failed to serialize clarity name: too long"
251-
);
252-
}
253-
254-
// the first byte is the length of the buffer.
255-
#[test_case(vec![4, 0xFF, 0xFE, 0xFD, 0xFC].as_slice(), "Failed to parse Clarity name: could not contruct from utf8"; "invalid_utf8")]
256-
#[test_case(vec![2, b'2', b'i'].as_slice(), "Failed to parse Clarity name: InvalidClarityName(\"ClarityName\", \"2i\")"; "invalid_name")] // starts with number
257-
#[test_case(vec![MAX_STRING_LEN + 1].as_slice(), "Failed to deserialize clarity name: too long"; "too_long")]
258-
#[test_case(vec![3, b'a'].as_slice(), "failed to fill whole buffer"; "wrong_length")]
259-
fn test_clarity_name_deserialization_errors<R: Read>(mut buffer: R, error_message: &str) {
260-
let result = ClarityName::consensus_deserialize(&mut buffer);
261-
assert!(result.is_err());
262-
assert_eq!(result.unwrap_err().to_string(), error_message);
263-
}
264-
265-
#[test_case("hello"; "valid_name")]
266-
#[test_case("contract-name"; "dash")]
267-
#[test_case("hello_world"; "underscore")]
268-
#[test_case("test123"; "numbers")]
269-
#[test_case("__transient"; "transient")]
270-
#[test_case("a"; "min_length")]
271-
#[test_case(&"a".repeat(CONTRACT_MAX_NAME_LENGTH); "max_length")]
272-
#[test_case(&"a".repeat(MAX_STRING_LEN as usize); "max_string_len")]
273-
fn test_contract_name_valid(name: &str) {
274-
let contract_name = ContractName::try_from(name.to_string())
275-
.unwrap_or_else(|_| panic!("Should parse valid contract name: {name}"));
276-
assert_eq!(contract_name.as_str(), name);
277-
}
278-
279-
#[test_case(""; "emtpy")]
280-
#[test_case("123contract"; "starts_with_number")]
281-
#[test_case("hello world"; "contains_space")]
282-
#[test_case("hello@world"; "contains_at")]
283-
#[test_case("hello.world"; "contains_dot")]
284-
#[test_case("hello!world"; "contains_exclamation")]
285-
#[test_case("hello?world"; "contains_question")]
286-
#[test_case("hello+world"; "contains_plus")]
287-
#[test_case("hello*world"; "contains_asterisk")]
288-
#[test_case("hello=world"; "contains_equals")]
289-
#[test_case("hello/world"; "contains_slash")]
290-
#[test_case("hello<world"; "contains_less_than")]
291-
#[test_case("hello>world"; "contains_greater_than")]
292-
#[test_case("hello,world"; "contains_comma")]
293-
#[test_case("hello;world"; "contains_semicolon")]
294-
#[test_case("hello:world"; "contains_colon")]
295-
#[test_case("hello|world"; "contains_pipe")]
296-
#[test_case("hello\\world"; "contains_backslash")]
297-
#[test_case("hello\"world"; "contains_quote")]
298-
#[test_case("hello'world"; "contains_apostrophe")]
299-
#[test_case("hello[world"; "contains_bracket_open")]
300-
#[test_case("hello]world"; "contains_bracket_close")]
301-
#[test_case("hello{world"; "contains_curly_open")]
302-
#[test_case("hello}world"; "contains_curly_close")]
303-
#[test_case("hello(world"; "contains_parenthesis_open")]
304-
#[test_case("hello)world"; "contains_parenthesis_close")]
305-
#[test_case(&"a".repeat(MAX_STRING_LEN as usize + 1); "too_long")]
306-
fn test_contract_name_invalid(name: &str) {
307-
let result = ContractName::try_from(name.to_string());
308-
assert!(result.is_err());
309-
assert!(matches!(
310-
result.unwrap_err(),
311-
CodecError::InvalidContractName(_, _)
312-
));
313-
}
314-
315-
#[test_case("test-contract"; "valid_name")]
316-
#[test_case("contract-name"; "dash")]
317-
#[test_case("hello_world"; "underscore")]
318-
#[test_case("test123"; "numbers")]
319-
#[test_case("__transient"; "transient")]
320-
#[test_case("a"; "min_length")]
321-
#[test_case(&"a".repeat(CONTRACT_MAX_NAME_LENGTH); "max_length")]
322-
fn test_contract_name_serialization(name: &str) {
323-
let name = ContractName::try_from(name.to_string()).unwrap();
324-
let mut buffer = Vec::with_capacity((name.len() + 1) as usize);
325-
name.consensus_serialize(&mut buffer)
326-
.unwrap_or_else(|_| panic!("Serialization should succeed for name: {name}"));
327-
assert_eq!(buffer[0], name.len());
328-
assert_eq!(&buffer[1..], name.as_bytes());
329-
330-
// Test deserialization
331-
let deserialized = ContractName::consensus_deserialize(&mut buffer.as_slice()).unwrap();
332-
assert_eq!(deserialized, name);
333-
}
334-
335-
#[test_case(&"a".repeat(CONTRACT_MIN_NAME_LENGTH - 1); "too_short")]
336-
#[test_case(&"a".repeat(CONTRACT_MAX_NAME_LENGTH + 1); "too_long")]
337-
#[test_case(&"a".repeat(MAX_STRING_LEN as usize); "max_string_len")]
338-
fn test_contract_name_serialization_too_long_or_short(name: &str) {
339-
let name = ContractName(name.to_string());
340-
let mut buffer = Vec::with_capacity((name.len() + 1) as usize);
341-
let result = name.consensus_serialize(&mut buffer);
342-
assert!(result.is_err());
343-
assert_eq!(
344-
result.unwrap_err().to_string(),
345-
format!(
346-
"Failed to serialize contract name: too short or too long: {}",
347-
name.len()
348-
)
349-
);
350-
}
351-
352-
// the first byte is the length of the buffer.
353-
#[test_case(vec![4, 0xFF, 0xFE, 0xFD, 0xFC].as_slice(), "Failed to parse Contract name: could not construct from utf8"; "invalid_utf8")]
354-
#[test_case(vec![2, b'2', b'i'].as_slice(), "Failed to parse Contract name: InvalidContractName(\"ContractName\", \"2i\")"; "invalid_name")] // starts with number
355-
#[test_case(vec![MAX_STRING_LEN + 1].as_slice(), &format!("Failed to deserialize contract name: too short or too long: {}", MAX_STRING_LEN + 1); "too_long")]
356-
#[test_case(vec![3, b'a'].as_slice(), "failed to fill whole buffer"; "wrong_length")]
357-
fn test_contract_name_deserialization_errors<R: Read>(mut buffer: R, error_message: &str) {
358-
let result = ContractName::consensus_deserialize(&mut buffer);
359-
assert!(result.is_err());
360-
assert_eq!(result.unwrap_err().to_string(), error_message);
361-
}
362-
}

clarity-serialization/src/tests/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
//
1313
// You should have received a copy of the GNU General Public License
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
mod types;
1516

1617
use stacks_common::address::{AddressHashMode, C32_ADDRESS_VERSION_TESTNET_SINGLESIG};
1718
use stacks_common::types::chainstate::{StacksAddress, StacksPrivateKey, StacksPublicKey};
1819

1920
use crate::errors::CodecError;
2021
use crate::types::{PrincipalData, StandardPrincipalData, Value};
22+
use crate::{BUILD_TYPE, version_string};
2123

2224
impl Value {
2325
pub fn list_from(list_data: Vec<Value>) -> Result<Value, CodecError> {
@@ -58,3 +60,17 @@ impl From<&StacksPrivateKey> for Value {
5860
Value::from(StandardPrincipalData::from(o))
5961
}
6062
}
63+
64+
#[test]
65+
fn test_version_string_basic_no_env() {
66+
let version = version_string("test-package", "1.0.0");
67+
68+
assert_eq!(
69+
version,
70+
format!(
71+
"test-package 1.0.0 (:, {BUILD_TYPE} build, {} [{}])",
72+
std::env::consts::OS,
73+
std::env::consts::ARCH
74+
)
75+
);
76+
}

0 commit comments

Comments
 (0)