Skip to content

Commit

Permalink
1.1.0 - Breaking change! copywith / change to, the Opt class has been…
Browse files Browse the repository at this point in the history
… removed and now we favour the () => syntax for optional parameters

1.0.8 - change_to added for a subclass to change the type back to a superclass
  • Loading branch information
atreeon committed Dec 12, 2023
1 parent c9e79cb commit ec9c989
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 136 deletions.
5 changes: 5 additions & 0 deletions example/test/deleteMe.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'ex21_custom_constructors_test.dart' as A;

main(){
A.A_Factory();
}
7 changes: 4 additions & 3 deletions example/test/ex11_generic_subclass_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:test/test.dart';
import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

part 'ex11_generic_subclass_test.morphy.dart';

Expand All @@ -8,6 +8,7 @@ part 'ex11_generic_subclass_test.morphy.dart';
@morphy
abstract class $$A<T1, T2> {
T1 get x;

T2 get y;

const $$A();
Expand All @@ -31,13 +32,13 @@ main() {

test("1 ba copy with", () {
var b = B(x: 1, y: "y", z: "z");
var ba_copy = b.copyWith_A(x: Opt(2), y: Opt("Y"));
var ba_copy = b.copyWith_A(x: () => 2, y: () => "Y");
expect(ba_copy.toString(), "(B-x:2|y:Y|z:z)");
});

test("2 bb copy with", () {
var b = B(x: 1, y: "y", z: "z");
var bb_copy = b.copyWith_B(x: Opt(2), y: Opt("Y"), z: Opt("Z"));
var bb_copy = b.copyWith_B(x: () => 2, y: () => "Y", z: () => "Z");
expect(bb_copy.toString(), "(B-x:2|y:Y|z:Z)");
});
}
13 changes: 7 additions & 6 deletions example/test/ex13_copywith_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

part 'ex13_copywith_test.morphy.dart';
// part 'ex13_copywith_test.morphy_manual.dart';

// ignore_for_file: UNNECESSARY_CAST

Expand All @@ -28,28 +29,28 @@ main() {
var b = B(a: "A", b: 1);
var c = C(a: "A", b: 1, c: true);

var a1 = a.copyWith_A(a: Opt("Aa1"));
var a1 = a.copyWith_A(a: () => "Aa1");
expect(a1.a, "Aa1");

var b1 = b.copyWith_A(a: Opt("Ab1"));
var b1 = b.copyWith_A(a: () => "Ab1");
expect(b1.a, "Ab1");
expect((b1 as B).b, 1);

var b2 = b.copyWith_B(a: Opt("Ab2"), b: Opt(2));
var b2 = b.copyWith_B(a: () => "Ab2", b: () => 2);
expect(b2.a, "Ab2");
expect(b2.b, 2);

var c1 = c.copyWith_A(a: Opt("Ac1"));
var c1 = c.copyWith_A(a: () => "Ac1");
expect(c1.a, "Ac1");
expect((c1 as C).b, 1);
expect((c1).c, true);

var c2 = c.copyWith_B(a: Opt("Ac1"), b: Opt(3));
var c2 = c.copyWith_B(a: () => "Ac1", b: () => 3);
expect(c2.a, "Ac1");
expect(c2.b, 3);
expect((c2 as C).c, true);

var c3 = c.copyWith_C(a: Opt("Ac1"), b: Opt(3), c: Opt(false));
var c3 = c.copyWith_C(a: () => "Ac1", b: () => 3, c: () => false);
expect(c3.a, "Ac1");
expect(c3.b, 3);
expect(c3.c, false);
Expand Down
5 changes: 5 additions & 0 deletions example/test/ex21_custom_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ part 'ex21_custom_constructors_test.morphy.dart';
@Morphy(hidePublicConstructor: true)
abstract class $A {
String get a;

$A blah(String a) {
return A._(a: a);
}
}

void main() {
test("0 default value", () {
var a = A._(a: "my default value");

expect(a.a, "my default value");
});

Expand Down
24 changes: 14 additions & 10 deletions example/test/ex29_copywith_subclasses_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:test/test.dart';
import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

// ignore_for_file: UNNECESSARY_CAST

Expand All @@ -17,20 +17,24 @@ abstract class $$A {
@morphy
abstract class $B<T1> implements $$A {
String get a;

T1 get b;
}

//subclass of B with same generic
@morphy
abstract class $C<T1> implements $B<T1> {
String get a;

T1 get b;

bool get c;
}

@morphy
abstract class $D<T1> implements $B<T1> {
String get a;

T1 get b;
}

Expand All @@ -45,49 +49,49 @@ abstract class $Y implements $$X {
main() {
test("ba", () {
A ba = B(b: 5, a: "A");
A ba_copy = ba.copyWith_A(a: Opt("a"));
A ba_copy = ba.copyWith_A(a: () => "a");
expect(ba_copy.toString(), "(B-a:a|b:5)");
});

test("bb", () {
B bb = B(b: 5, a: "A");
B bb_copy = bb.copyWith_B(a: Opt("a"), b: Opt(6));
B bb_copy = bb.copyWith_B(a: () => "a", b: () => 6);
expect(bb_copy.toString(), "(B-a:a|b:6)");
});

test("ca", () {
A ca = C(b: 5, a: "A", c: true);
A ca_copy = ca.copyWith_A(a: Opt("a"));
A ca_copy = ca.copyWith_A(a: () => "a");
expect(ca_copy.toString(), "(C-a:a|b:5|c:true)");
});

test("cb", () {
B cb = C(b: 5, a: "A", c: true);
B cb_copy = cb.copyWith_B(a: Opt("a"), b: Opt(6));
B cb_copy = cb.copyWith_B(a: () => "a", b: () => 6);
expect(cb_copy.toString(), "(C-a:a|b:6|c:true)");
});

test("cc", () {
C cc = C(b: 5, a: "A", c: true);
var cc_copy = cc.copyWith_C(a: Opt("a"), b: Opt(6), c: Opt(false));
var cc_copy = cc.copyWith_C(a: () => "a", b: () => 6, c: () => false);
expect(cc_copy.toString(), "(C-a:a|b:6|c:false)");
});

test("da", () {
D da = D(b: 5, a: "A");
var da_copy = da.copyWith_A(a: Opt("a"));
var da_copy = da.copyWith_A(a: () => "a");
expect(da_copy.toString(), "(D-a:a|b:5)");
});

test("db", () {
D db = D(b: 5, a: "A");
var db_copy = db.copyWith_B(a: Opt("a"), b: Opt(6));
var db_copy = db.copyWith_B(a: () => "a", b: () => 6);
expect(db_copy.toString(), "(D-a:a|b:6)");
});

test("dd", () {
D dd = D(b: 5, a: "A");
var dd_copy = dd.copyWith_D(a: Opt("a"), b: Opt(6));
var dd_copy = dd.copyWith_D(a: () => "a", b: () => 6);
expect(dd_copy.toString(), "(D-a:a|b:6)");
});

Expand All @@ -99,7 +103,7 @@ main() {

test("yY", () {
Y yy = Y(a: "A");
var yy_copy = yy.copyWith_Y(a: Opt("a"));
var yy_copy = yy.copyWith_Y(a: () => "a");
expect(yy_copy.toString(), "(Y-a:a)");
});
}
5 changes: 5 additions & 0 deletions example/test/ex31_constant_constructor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ main() {
var a = A.constant(a: 1);

expect(a.a, 1);

//you can't change a constant object but you can copy and then change one
var copya = a.copyWith_A();

expect(copya.a, 1);
});
}
2 changes: 1 addition & 1 deletion example/test/ex38_cw_vs_copyTo_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main() {
expect(b_copy1.toString(), "(SubB-z:z|cs:[(C-m:m)]|x:x)");

//copywith from SubB to SubB (traditional copy with)
var b_copy2 = b.copyWith_SubB(cs: Opt([C(m: "m2")]));
var b_copy2 = b.copyWith_SubB(cs: () => [C(m: "m2")]);
expect(b_copy2.toString(), "(SubB-z:z|cs:[(C-m:m2)]|x:x)");

//copy TO from one sibling to another
Expand Down
2 changes: 1 addition & 1 deletion example/test/ex45_copyWithPolymorphic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ main() {

//copyWith_Super called on both Super & Sub objects
var result = supers.map((e) => //
e.copyWith_Super(id: Opt(e.id + "_"))).toList();
e.copyWith_Super(id: () => e.id + "_")).toList();

//they both retain their original type
expect(result[0] is Sub, false);
Expand Down
42 changes: 42 additions & 0 deletions example/test/ex57_change_to_parent_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element

import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

part 'ex57_change_to_parent_test.morphy.dart';

main() {
test("1 toJson", () {
var aList = [
A(id: "1"),
B(id: "2", blah: "sdf"),
C(id: "3", xyz: "my custom"),
];

var result = aList.map((e) => e.changeTo_A()).toList();

final expected = [
A(id: "1"),
A(id: "2"),
A(id: "3"),
];

expect(result, expected);
});
}

@Morphy(explicitSubTypes: [$B, $C])
abstract class $A {
String get id;
}

@Morphy()
abstract class $B implements $A {
String get blah;
}

@Morphy()
abstract class $C implements $A {
String get xyz;
}
46 changes: 46 additions & 0 deletions example/test/ex58_copywith_nullable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

part 'ex58_copywith_nullable.morphy.dart';

//nullable types and copyWith

@morphy
abstract class $Pet {
String? get type;

String get name;
}

main() {
test("1", () {
var a = Pet(type: "cat", name: "bob");

var expected = Pet(type: "cat", name: "bob");
expect(a, expected);

//set type to null
var a_copy = a.copyWith_Pet(type: () => null);

var expected2 = Pet(type: null, name: "bob");
expect(a_copy, expected2);

//copy just one param
var a_copy3 = a.copyWith_Pet(name: () => "bobby");

var expected3 = Pet(type: "cat", name: "bobby");
expect(a_copy3, expected3);

//set type to null initially
var a4 = Pet(type: null, name: "bob");

var expected4 = Pet(type: null, name: "bob");
expect(a4, expected4);

//type not specified
var a5 = Pet(name: "bob");

var expected5 = Pet(type: null, name: "bob");
expect(a5, expected5);
});
}
12 changes: 6 additions & 6 deletions example/test/ex7_override_properties_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ main() {

test("2 aa copy with", () {
var a = A(a: Person("1"));
var a_copy = a.copyWith_A(a: Opt(Person("X")));
var a_copy = a.copyWith_A(a: () => Person("X"));
expect(a_copy.toString(), "(A-a:X)");
});

test("2 ba copy with", () {
var b = B(a: Employee("1", "2"));
var ba_copy = b.copyWith_A(a: Opt(Employee("X", "Y")));
var ba_copy = b.copyWith_A(a: () => Employee("X", "Y"));
expect(ba_copy.toString(), "(B-a:X|Y)");
});

test("3 bb copy with", () {
var b = B(a: Employee("1", "2"));
var bb_copy = b.copyWith_B(a: Opt(Employee("X", "Y")));
var bb_copy = b.copyWith_B(a: () => Employee("X", "Y"));
expect(bb_copy.toString(), "(B-a:X|Y)");
});

test("4 ca copy with", () {
var c = C(a: Manager("1", "2", "3"));
var ca_copy = c.copyWith_A(a: Opt(Manager("X", "Y", "Z")));
var ca_copy = c.copyWith_A(a: () => Manager("X", "Y", "Z"));
expect(ca_copy.toString(), "(C-a:X|Y|Z)");
});

test("5 cb copy with", () {
var c = C(a: Manager("1", "2", "3"));
var cb_copy = c.copyWith_B(a: Opt(Manager("X", "Y", "Z")));
var cb_copy = c.copyWith_B(a: () => Manager("X", "Y", "Z"));
expect(cb_copy.toString(), "(C-a:X|Y|Z)");
});

test("6 cc copy with", () {
var c = C(a: Manager("1", "2", "3"));
var cc_copy = c.copyWith_C(a: Opt(Manager("X", "Y", "Z")));
var cc_copy = c.copyWith_C(a: () => Manager("X", "Y", "Z"));
expect(cc_copy.toString(), "(C-a:X|Y|Z)");
});
}
Expand Down
8 changes: 4 additions & 4 deletions example/test/readme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ main() {

test("3 CopyWith simple", () {
var flossy = Pet(name: "Flossy", age: 5);
var plossy = flossy.copyWith_Pet(name: Opt("Plossy"));
var plossy = flossy.copyWith_Pet(name: () => "Plossy");

expect(flossy.age, plossy.age);
});
Expand All @@ -110,7 +110,7 @@ main() {
];

var petsOlder = pets //
.map((e) => e.copyWith_Pet(age: Opt(e.age + 1)))
.map((e) => e.copyWith_Pet(age: () => e.age + 1))
.toList();

expect(petsOlder[0].age, 5);
Expand Down Expand Up @@ -151,12 +151,12 @@ main() {
expect(frankie is Dog, true);
});

A a_Constructor(String val) {
A A_FactoryFunction(String val) {
return A._(val: val, timestamp: DateTime(2023, 11, 25));
}

test("11 Custom Constructor", () {
var a = a_Constructor("my value");
var a = A_FactoryFunction("my value");

expect(a.timestamp, DateTime(2023, 11, 25));
});
Expand Down
Loading

0 comments on commit ec9c989

Please sign in to comment.