// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. import 'package:test/test.dart'; import 'package:_macros/src/executor/cast.dart'; void main() { test("dynamic casts", () { expect(Cast().cast(3), 3); expect(Cast().cast("hello"), "hello"); }); test("int casts", () { expect(Cast().cast(3), 3); expect( () => Cast().cast("hello"), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type int but got type String for: hello'))); }); test("double casts", () { expect(Cast().cast(3.1), 3.1); expect( () => Cast().cast("hello"), throwsA(isA().having( (e) => e.toString(), 'toString()', 'Failed cast: ' 'expected type double but got type String for: hello'))); }); test("String casts", () { expect(Cast().cast("hello"), "hello"); expect( () => Cast().cast(3), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("bool casts", () { expect(Cast().cast(true), true); expect( () => Cast().cast(3), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type bool but got type int for: 3'))); }); test("Casting empty lists", () { var listOfInt = ListCast.from(Cast()); var listOfString = ListCast.from(Cast()); expect(listOfInt.cast([]) is List, isTrue); expect(listOfInt.cast([]), []); expect( () => listOfString.cast({}), throwsA(isA().having( (e) => e.toString(), 'toString()', 'Failed cast: expected type List but got ' 'type _Map for: {}'))); }); test("Casting non-empty lists", () { var listOfInt = ListCast.from(Cast()); var listOfString = ListCast.from(Cast()); expect(listOfInt.cast([3]) is List, isTrue); expect(listOfInt.cast([3]), [3]); expect( () => listOfString.cast([3]), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting nested lists", () { var listOfInt = ListCast.from(Cast()); var listOfString = ListCast.from(Cast()); var listOfListOfInt = ListCast.from(listOfInt); var listOfListOfString = ListCast.from(listOfString); expect( listOfListOfInt.cast([ [3] ]) is List>, isTrue); expect( listOfListOfInt.cast([ [3] ]), >[ [3] ]); expect( () => listOfListOfString.cast([ [3] ]), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting non-empty sets", () { var setOfInt = SetCast.from(Cast()); var setOfString = SetCast.from(Cast()); expect(setOfInt.cast({3}) is Set, isTrue); expect(setOfInt.cast({3}), {3}); expect( () => setOfString.cast({3}), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting nested sets", () { var setOfInt = SetCast.from(Cast()); var setOfString = SetCast.from(Cast()); var setOfSetOfInt = SetCast.from(setOfInt); var setOfSetOfString = SetCast.from(setOfString); expect( setOfSetOfInt.cast({ {3} }) is Set>, isTrue); expect( setOfSetOfInt.cast({ {3} }), >{ {3} }); expect( () => setOfSetOfString.cast({ {3} }), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting empty maps", () { var mapOfStringToInt = MapCast.from(Cast(), Cast()); var mapOfStringToString = MapCast.from(Cast(), Cast()); expect(mapOfStringToInt.cast({}) is Map, isTrue); expect(mapOfStringToInt.cast({}), {}); expect( () => mapOfStringToString.cast([]), throwsA(isA().having( (e) => e.toString(), 'toString()', 'Failed cast: expected type Map but got type ' 'List for: []'))); }); test("Casting non-empty maps", () { var mapOfStringToInt = MapCast.from(Cast(), Cast()); var mapOfStringToString = MapCast.from(Cast(), Cast()); expect( mapOfStringToInt.cast({"hello": 3}) is Map, isTrue); expect(mapOfStringToInt.cast({"hello": 3}), {"hello": 3}); expect(() => mapOfStringToString.cast({"hello": 3}), throwsA(isA())); expect( () => mapOfStringToString.cast({3: "world"}), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting nested maps", () { var schema = MapCast.from(Cast(), MapCast.from(Cast(), Cast())); expect( schema.cast({ "hello": {"hello": 3} }) is Map>, isTrue); expect( schema.cast({ "hello": {"hello": 3} }), >{ "hello": {"hello": 3} }); expect( () => schema.cast({ "hello": {3: "hello"} }), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type String but got type int for: 3'))); }); test("Casting nested list/maps/sets", () { var schema = MapCast.from(Cast(), ListCast.from(SetCast.from(Cast()))); expect( schema.cast({ "hello": [ {3} ] }) is Map>>, isTrue); expect( schema.cast({ "hello": [ {3} ] }), >>{ "hello": >[ {3} ] }); }); test("nullable cast", () { expect(Cast().nullable.cast(null), null); expect(Cast().nullable.cast(3), 3); expect(ListCast.from(Cast()).nullable.cast([3]), [3]); expect(ListCast.from(Cast().nullable).cast([3, null]), [3, null]); expect( () => Cast().nullable.cast(2.0), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type int but got type double for: 2.0'))); expect( () => ListCast.from(Cast()).nullable.cast([2.0]), throwsA(isA().having((e) => e.toString(), 'toString()', 'Failed cast: expected type int but got type double for: 2.0'))); }); }