1 module ut.array; 2 3 version (unittest): 4 5 import unit_threaded; 6 import spasm.dom; 7 import spasm.spa; 8 import spasm.types; 9 import spasm.rt.array; 10 11 @safe: 12 13 enum Filter { 14 All, 15 Checked, 16 Unchecked 17 } 18 19 struct Item { 20 mixin Node!"div"; 21 bool checked; 22 @prop key = 0; 23 this(bool c, int k) { 24 this.checked = c; 25 this.key = k; 26 } 27 } 28 29 struct Container { 30 nothrow: 31 mixin Node!"section"; 32 @child UnorderedList!Item list; 33 Filter filter; 34 DynamicArray!(Item*) items; 35 auto transform(ref DynamicArray!(Item*) items, Filter filter) @trusted { 36 with (Filter) { 37 final switch(filter) { 38 case All: 39 return items[].update(list); 40 case Checked: 41 import std.algorithm : filter ; 42 return items[].filter!(i=>i.checked).update(list); 43 case Unchecked: 44 import std.algorithm : filter ; 45 return items[].filter!(i=>!i.checked).update(list); 46 } 47 } 48 } 49 } 50 51 unittest { 52 renderToString!Container.should == "<section><ul></ul></section>"; 53 } 54 55 unittest { 56 Container container; 57 auto node = container.renderToNode; 58 node.renderToString().should == "<section><ul></ul></section>"; 59 container.items.put(new Item(true, 1)); 60 container.update!(container.items); 61 node.renderToString().should == "<section><ul><div key=1></div></ul></section>"; 62 container.update.filter = Filter.Unchecked; 63 node.renderToString().should == "<section><ul></ul></section>"; 64 } 65 66 unittest { 67 Container container; 68 auto node = container.renderToNode; 69 foreach(i; 1..3) 70 container.items.put(new Item(true, i)); 71 foreach(i; 3..6) 72 container.items.put(new Item(false, i)); 73 foreach(i; 6..9) 74 container.items.put(new Item(true, i)); 75 container.update!(container.items); 76 node.renderToString().should == "<section><ul><div key=1></div><div key=2></div><div key=3></div><div key=4></div><div key=5></div><div key=6></div><div key=7></div><div key=8></div></ul></section>"; 77 container.update.filter = Filter.Checked; 78 node.renderToString().should == "<section><ul><div key=1></div><div key=2></div><div key=6></div><div key=7></div><div key=8></div></ul></section>"; 79 container.update.filter = Filter.Unchecked; 80 node.renderToString().should == "<section><ul><div key=3></div><div key=4></div><div key=5></div></ul></section>"; 81 container.update.filter = Filter.All; 82 node.renderToString().should == "<section><ul><div key=1></div><div key=2></div><div key=3></div><div key=4></div><div key=5></div><div key=6></div><div key=7></div><div key=8></div></ul></section>"; 83 container.update.filter = Filter.Unchecked; 84 node.renderToString().should == "<section><ul><div key=3></div><div key=4></div><div key=5></div></ul></section>"; 85 container.update.filter = Filter.Checked; 86 node.renderToString().should == "<section><ul><div key=1></div><div key=2></div><div key=6></div><div key=7></div><div key=8></div></ul></section>"; 87 container.update.filter = Filter.All; 88 node.renderToString().should == "<section><ul><div key=1></div><div key=2></div><div key=3></div><div key=4></div><div key=5></div><div key=6></div><div key=7></div><div key=8></div></ul></section>"; 89 }