@@ -832,6 +832,93 @@ keyPath.test("tuple key path") {
832
832
expectNotNil ( t5 as? ReferenceWritableKeyPath < TupleProperties , String > )
833
833
}
834
834
835
+ keyPath. test ( " tuple key path execution " ) {
836
+ typealias T0 = ( Int , String )
837
+ typealias T1 = ( x: Int , y: String )
838
+
839
+ let kp_t0_0 = \T0 . 0
840
+ let kp_t0_1 = \T0 . 1
841
+
842
+ let kp_t1_x = \T1 . x
843
+ let kp_t1_y = \T1 . y
844
+ let kp_t1_0 = \T1 . 0
845
+ let kp_t1_1 = \T1 . 1
846
+
847
+ var tuple0 = ( 1 , " Hello " )
848
+ let tuple1 = ( x: 2 , y: " World " )
849
+ let tuple2 = ( a: 3 , b: " String " )
850
+
851
+ // in some cases, tuple key paths are interchangeable
852
+
853
+ expectEqual ( tuple0 [ keyPath: kp_t0_0] , 1 )
854
+ expectEqual ( tuple0 [ keyPath: kp_t1_x] , 1 )
855
+ expectEqual ( tuple0 [ keyPath: kp_t1_0] , 1 )
856
+
857
+ expectEqual ( tuple0 [ keyPath: kp_t0_1] , " Hello " )
858
+ expectEqual ( tuple0 [ keyPath: kp_t1_y] , " Hello " )
859
+ expectEqual ( tuple0 [ keyPath: kp_t1_1] , " Hello " )
860
+
861
+
862
+ expectEqual ( tuple1 [ keyPath: kp_t0_0] , 2 )
863
+ expectEqual ( tuple1 [ keyPath: kp_t1_x] , 2 )
864
+ expectEqual ( tuple1 [ keyPath: kp_t1_0] , 2 )
865
+
866
+ expectEqual ( tuple1 [ keyPath: kp_t0_1] , " World " )
867
+ expectEqual ( tuple1 [ keyPath: kp_t1_y] , " World " )
868
+ expectEqual ( tuple1 [ keyPath: kp_t1_1] , " World " )
869
+
870
+
871
+ expectEqual ( tuple2 [ keyPath: kp_t0_0] , 3 )
872
+ //expectEqual(tuple2[keyPath: kp_t1_x], 3)
873
+ //expectEqual(tuple2[keyPath: kp_t1_0], 3)
874
+
875
+ expectEqual ( tuple2 [ keyPath: kp_t0_1] , " String " )
876
+ //expectEqual(tuple2[keyPath: kp_t1_y], "String")
877
+ //expectEqual(tuple2[keyPath: kp_t1_1], "String")
878
+
879
+
880
+ tuple0 [ keyPath: kp_t0_1] = " Another String "
881
+ expectEqual ( tuple0 [ keyPath: kp_t0_1] , " Another String " )
882
+ }
883
+
884
+ keyPath. test ( " tuple key path execution (generic) " ) {
885
+ struct Container < T, U> {
886
+ let x : ( T , U )
887
+ var y : ( a: T , b: U )
888
+ }
889
+
890
+ func generic< A: Equatable , B: Equatable > ( a: A , b: B ) {
891
+ typealias T = ( A , B )
892
+
893
+ let kp_t_0 = \T . 0
894
+ let kp_t_1 = \T . 1
895
+
896
+ let kp_c_x = \Container < A , B > . x
897
+ let kp_c_x_0 = kp_c_x. appending ( path: kp_t_0)
898
+ let kp_c_x_1 = kp_c_x. appending ( path: kp_t_1)
899
+
900
+ let kp_c_y_a = \Container < A , B > . y. a
901
+ let kp_c_y_b = \Container < A , B > . y. b
902
+
903
+
904
+ let tuple = ( a, b)
905
+ let container = Container ( x: ( a, b) , y: ( a, b) )
906
+
907
+
908
+ expectEqual ( tuple [ keyPath: kp_t_0] , tuple. 0 )
909
+ expectEqual ( tuple [ keyPath: kp_t_1] , tuple. 1 )
910
+
911
+ expectEqual ( container [ keyPath: kp_c_x_0] , container. x. 0 )
912
+ expectEqual ( container [ keyPath: kp_c_x_1] , container. x. 1 )
913
+
914
+ expectEqual ( container [ keyPath: kp_c_y_a] , container. y. 0 )
915
+ expectEqual ( container [ keyPath: kp_c_y_b] , container. y. 1 )
916
+ }
917
+
918
+ generic ( a: 13 , b: " Hello Tuples " )
919
+ generic ( a: " Tuples Hello " , b: 31 )
920
+ }
921
+
835
922
keyPath. test ( " let-ness " ) {
836
923
expectNil ( \C < Int > . immutable as? ReferenceWritableKeyPath )
837
924
expectNotNil ( \C < Int > . secretlyMutable as? ReferenceWritableKeyPath )
0 commit comments