Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bunch of new tests for various literal data types #18

Merged
merged 9 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions test/data_types/byte_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:rdf_dart/src/data_types/byte.dart';
import 'package:test/test.dart';

void main() {
group('ByteCodec', () {
group('Encoder (String to int)', () {
test('should parse valid byte strings within range', () {
expect(byte.encoder.convert('0'), 0);
expect(byte.encoder.convert('127'), 127);
expect(byte.encoder.convert('-128'), -128);
expect(byte.encoder.convert('10'), 10);
expect(byte.encoder.convert('-10'), -10);
expect(byte.encoder.convert('+50'), 50); // With explicit plus
});

test('should handle whitespace correctly (collapse)', () {
expect(byte.encoder.convert(' 10 '), 10);
expect(byte.encoder.convert('\t+50\n'), 50);
// Note: processWhiteSpace replaces \t,\n,\r with space, collapses, trims
expect(
byte.encoder.convert(' -30 '),
-30,
); // Space between sign and number might fail pattern
});

test('should throw RangeError for valid integers outside byte range', () {
// Just outside range
expect(() => byte.encoder.convert('128'), throwsRangeError);
expect(() => byte.encoder.convert('-129'), throwsRangeError);
// Way outside range
expect(() => byte.encoder.convert('1000'), throwsRangeError);
expect(() => byte.encoder.convert('-1000'), throwsRangeError);
});

test('should throw FormatException for invalid formats', () {
expect(
() => byte.encoder.convert(''),
throwsFormatException,
); // Empty string
expect(
() => byte.encoder.convert('abc'),
throwsFormatException,
); // Non-numeric
expect(
() => byte.encoder.convert('12.0'),
throwsFormatException,
); // Decimal point
expect(
() => byte.encoder.convert('1e2'),
throwsFormatException,
); // Exponent
expect(
() => byte.encoder.convert('--10'),
throwsFormatException,
); // Double sign
expect(
() => byte.encoder.convert('+-10'),
throwsFormatException,
); // Mixed signs
expect(
() => byte.encoder.convert('1 0'),
throwsFormatException,
); // Internal space after collapse
});
});

// --- Decoder Tests (int -> String) ---
group('Decoder (int to String)', () {
test('should format valid byte integers to string', () {
expect(byte.decoder.convert(0), '0');
expect(byte.decoder.convert(127), '127');
expect(byte.decoder.convert(-128), '-128');
expect(byte.decoder.convert(10), '10');
expect(byte.decoder.convert(-10), '-10');
});

test('should throw RangeError for integers outside byte range', () {
expect(() => byte.decoder.convert(128), throwsRangeError);
expect(() => byte.decoder.convert(-129), throwsRangeError);
expect(() => byte.decoder.convert(1000), throwsRangeError);
expect(() => byte.decoder.convert(-1000), throwsRangeError);
});
});
});
}
94 changes: 94 additions & 0 deletions test/data_types/int_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:rdf_dart/src/data_types/int.dart'; // Use intCodec from here
import 'package:test/test.dart';

void main() {
group('IntCodec (xsd:int)', () {
const minInt = -2147483648;
const maxInt = 2147483647;

// --- Encoder Tests (String -> int) ---
group('Encoder (String to int)', () {
test('should parse valid int strings within range', () {
expect(intCodec.encoder.convert('0'), 0);
expect(intCodec.encoder.convert('$maxInt'), maxInt);
expect(intCodec.encoder.convert('$minInt'), minInt);
expect(intCodec.encoder.convert('100000'), 100000);
expect(intCodec.encoder.convert('-100000'), -100000);
expect(intCodec.encoder.convert('+200'), 200); // With explicit plus
});

test('should handle whitespace correctly (collapse)', () {
expect(intCodec.encoder.convert(' 12345 '), 12345);
expect(intCodec.encoder.convert('\t-54321\n'), -54321);
});

test('should throw RangeError for valid integers outside int range', () {
// Use BigInt for numbers just outside the 32-bit range
final maxPlusOne = BigInt.from(maxInt) + BigInt.one;
final minMinusOne = BigInt.from(minInt) - BigInt.one;

expect(() => intCodec.encoder.convert('$maxPlusOne'), throwsRangeError);
expect(
() => intCodec.encoder.convert('$minMinusOne'),
throwsRangeError,
);

// Values definitely outside
expect(() => intCodec.encoder.convert('3000000000'), throwsRangeError);
expect(() => intCodec.encoder.convert('-3000000000'), throwsRangeError);
});

test('should throw FormatException for invalid formats', () {
expect(() => intCodec.encoder.convert(''), throwsFormatException);
expect(() => intCodec.encoder.convert('abc'), throwsFormatException);
expect(
() => intCodec.encoder.convert('12.0'),
throwsFormatException,
); // Decimal
expect(
() => intCodec.encoder.convert('1e5'),
throwsFormatException,
); // Exponent
expect(
() => intCodec.encoder.convert('--10'),
throwsFormatException,
); // Double sign
expect(
() => intCodec.encoder.convert('+-10'),
throwsFormatException,
); // Mixed signs
expect(
() => intCodec.encoder.convert('1 000'),
throwsFormatException,
); // Internal space
});
});

// --- Decoder Tests (int -> String) ---
group('Decoder (int to String)', () {
test('should format valid int integers to string', () {
expect(intCodec.decoder.convert(0), '0');
expect(intCodec.decoder.convert(maxInt), '$maxInt');
expect(intCodec.decoder.convert(minInt), '$minInt');
expect(intCodec.decoder.convert(12345), '12345');
expect(intCodec.decoder.convert(-54321), '-54321');
});

test('should throw RangeError for integers outside int range', () {
// Need to construct values outside the range carefully if platform int > 32 bits
// Using BigInt and checking if they fit in standard int might be needed,
// but for testing the *decoder's* check, we can try literals if they work.
// If Dart's int is 64-bit, these literals are valid ints but should fail the decoder's range check.
const maxPlusOne = 2147483648; // Outside 32-bit positive range
const minMinusOne = -2147483649; // Outside 32-bit negative range

expect(() => intCodec.decoder.convert(maxPlusOne), throwsRangeError);
expect(() => intCodec.decoder.convert(minMinusOne), throwsRangeError);

// Values definitely outside
expect(() => intCodec.decoder.convert(3000000000), throwsRangeError);
expect(() => intCodec.decoder.convert(-3000000000), throwsRangeError);
});
});
});
}
89 changes: 89 additions & 0 deletions test/data_types/negative_integer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'package:rdf_dart/src/data_types/negative_integer.dart';
import 'package:test/test.dart';

void main() {
group('NegativeIntegerCodec (xsd:negativeInteger)', () {
const maxNegInt = -1;
// Using standard int min as a practical lower bound example
const minInt = -2147483648;

// --- Encoder Tests (String -> int) ---
group('Encoder (String to int)', () {
test('should parse valid negative integer strings', () {
expect(negativeInteger.encoder.convert('-1'), maxNegInt);
expect(negativeInteger.encoder.convert('-10'), -10);
expect(
negativeInteger.encoder.convert('$minInt'),
minInt,
); // Min 32-bit int
expect(negativeInteger.encoder.convert('-123456789'), -123456789);
});

test('should handle whitespace correctly (collapse)', () {
expect(negativeInteger.encoder.convert(' -1 '), maxNegInt);
expect(negativeInteger.encoder.convert('\n-99\t'), -99);
});

test('should throw RangeError for zero or positive integers', () {
expect(() => negativeInteger.encoder.convert('0'), throwsRangeError);
expect(
() => negativeInteger.encoder.convert('+0'),
throwsRangeError,
); // Although +0 parses to 0
expect(() => negativeInteger.encoder.convert('1'), throwsRangeError);
expect(() => negativeInteger.encoder.convert('+1'), throwsRangeError);
expect(() => negativeInteger.encoder.convert('100'), throwsRangeError);
});

test('should throw FormatException for invalid formats', () {
expect(
() => negativeInteger.encoder.convert(''),
throwsFormatException,
);
expect(
() => negativeInteger.encoder.convert('abc'),
throwsFormatException,
);
expect(
() => negativeInteger.encoder.convert('-12.0'),
throwsFormatException,
); // Decimal
expect(
() => negativeInteger.encoder.convert('-1e5'),
throwsFormatException,
); // Exponent
expect(
() => negativeInteger.encoder.convert('--10'),
throwsFormatException,
); // Double sign
expect(
() => negativeInteger.encoder.convert('+-10'),
throwsFormatException,
); // Mixed signs (should fail pattern)
expect(
() => negativeInteger.encoder.convert('-1 000'),
throwsFormatException,
); // Internal space
});
});

// --- Decoder Tests (int -> String) ---
group('Decoder (int to String)', () {
test('should format valid negative integers to string', () {
expect(negativeInteger.decoder.convert(maxNegInt), '-1');
expect(negativeInteger.decoder.convert(-10), '-10');
expect(negativeInteger.decoder.convert(minInt), '$minInt');
expect(negativeInteger.decoder.convert(-12345), '-12345');
});

test('should throw RangeError for zero or positive integers', () {
expect(() => negativeInteger.decoder.convert(0), throwsRangeError);
expect(() => negativeInteger.decoder.convert(1), throwsRangeError);
expect(() => negativeInteger.decoder.convert(100), throwsRangeError);
// Test upper bound of standard int if relevant
const maxInt = 2147483647;
expect(() => negativeInteger.decoder.convert(maxInt), throwsRangeError);
});
});
});
}
97 changes: 97 additions & 0 deletions test/data_types/non_negative_integer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:rdf_dart/src/data_types/non_negative_integer.dart';
import 'package:test/test.dart';

void main() {
group('NonNegativeIntegerCodec (xsd:nonNegativeInteger)', () {
const minNonNegInt = 0;
// Using standard int max as a practical upper bound example
const maxInt = 2147483647;

// --- Encoder Tests (String -> int) ---
group('Encoder (String to int)', () {
test('should parse valid non-negative integer strings', () {
expect(nonNegativeInteger.encoder.convert('0'), minNonNegInt);
expect(
nonNegativeInteger.encoder.convert('+0'),
minNonNegInt,
); // Explicit plus zero
expect(nonNegativeInteger.encoder.convert('1'), 1);
expect(nonNegativeInteger.encoder.convert('10'), 10);
expect(nonNegativeInteger.encoder.convert('+10'), 10); // Explicit plus
expect(nonNegativeInteger.encoder.convert('$maxInt'), maxInt);
});

test('should handle whitespace correctly (collapse)', () {
expect(nonNegativeInteger.encoder.convert(' 0 '), 0);
expect(nonNegativeInteger.encoder.convert('\n123\t'), 123);
});

test('should throw RangeError for negative integers', () {
expect(
() => nonNegativeInteger.encoder.convert('-1'),
throwsRangeError,
);
expect(
() => nonNegativeInteger.encoder.convert('-10'),
throwsRangeError,
);
const minInt = -2147483648;
expect(
() => nonNegativeInteger.encoder.convert('$minInt'),
throwsRangeError,
);
});

test('should throw FormatException for invalid formats', () {
expect(
() => nonNegativeInteger.encoder.convert(''),
throwsFormatException,
);
expect(
() => nonNegativeInteger.encoder.convert('abc'),
throwsFormatException,
);
expect(
() => nonNegativeInteger.encoder.convert('1.0'),
throwsFormatException,
); // Decimal
expect(
() => nonNegativeInteger.encoder.convert('1e2'),
throwsFormatException,
); // Exponent
expect(
() => nonNegativeInteger.encoder.convert('--10'),
throwsFormatException,
); // Double sign (fails pattern)
expect(
() => nonNegativeInteger.encoder.convert('+-10'),
throwsFormatException,
); // Mixed signs (fails pattern)
expect(
() => nonNegativeInteger.encoder.convert('1 000'),
throwsFormatException,
); // Internal space
});
});

// --- Decoder Tests (int -> String) ---
group('Decoder (int to String)', () {
test('should format valid non-negative integers to string', () {
expect(nonNegativeInteger.decoder.convert(minNonNegInt), '0');
expect(nonNegativeInteger.decoder.convert(1), '1');
expect(nonNegativeInteger.decoder.convert(10), '10');
expect(nonNegativeInteger.decoder.convert(maxInt), '$maxInt');
});

test('should throw RangeError for negative integers', () {
expect(() => nonNegativeInteger.decoder.convert(-1), throwsRangeError);
expect(() => nonNegativeInteger.decoder.convert(-10), throwsRangeError);
const minInt = -2147483648;
expect(
() => nonNegativeInteger.decoder.convert(minInt),
throwsRangeError,
);
});
});
});
}
Loading