-
Notifications
You must be signed in to change notification settings - Fork 2
/
foxmock.prg
1993 lines (1651 loc) · 83.9 KB
/
foxmock.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*========================================================================================
* Generic Mockup Framework
*
* Written 2010 by Christof Wollenhaupt
*========================================================================================
Define Class foxMock as Collection
*--------------------------------------------------------------------------------------
* When an object accessor returns a new object reference, the object is automatically
* released before the next step - accessing the property - is executed. Therefore we
* need an additional reference to those temporary obejcts
*--------------------------------------------------------------------------------------
oKeepAlive = NULL
*--------------------------------------------------------------------------------------
* There's one master objects that keeps track of the test definition for what appears
* to be a single object to the user. This is the object returned by the new action.
* Action implementation need to be aware of the master and return it for the next
* access.
*--------------------------------------------------------------------------------------
oMaster = NULL
*--------------------------------------------------------------------------------------
* The repository is the first instance of the foxMock object that the user created.
* This is the place where all objects are located.
*--------------------------------------------------------------------------------------
oRepository = NULL
*--------------------------------------------------------------------------------------
* Context specific operations need to know on which member to work on.
*--------------------------------------------------------------------------------------
cActiveMember = ""
*--------------------------------------------------------------------------------------
* The object operates in to modes: definition and mockup. In definition mode we can
* call operations to specify the behavior of the object. This definition has to occur
* in a single line.
*--------------------------------------------------------------------------------------
cDefinitionLine = ""
*--------------------------------------------------------------------------------------
* In definition mode we may have a single expectation that all subsequent operations
* refer to. The repository keeps track of all expectations in a single location.
*--------------------------------------------------------------------------------------
oExpectation = NULL
Expectations = NULL
*--------------------------------------------------------------------------------------
* Every object provides a number of simulated members. In this collection we keep
* track of all members, call frequencies, etc.
*--------------------------------------------------------------------------------------
Members = Null
*--------------------------------------------------------------------------------------
* Many classes require that developers create a subclass and overide methods with the
* actual implementation. To test those without creating a sub class for every test,
* we can use these classes as the basis for our mock up object.
*--------------------------------------------------------------------------------------
cFoundation = ""
oFoundation = NULL
*--------------------------------------------------------------------------------------
* A list of all temporary program files that have been created for mocking members.
* Each name is separated by a line break.
*--------------------------------------------------------------------------------------
cTempFiles = ""
*========================================================================================
* Internally we instantiate subclasses that need to refer back to the master obejct and
* the repository object.
*========================================================================================
Procedure Init (toMaster, toRepository)
*--------------------------------------------------------------------------------------
* We need access to the physical object
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Initialize references
*--------------------------------------------------------------------------------------
This.Members = CreateObject("Collection")
This.Expectations = CreateObject("Collection")
If Vartype(m.toMaster) == "O"
This.oMaster = m.toMaster
EndIf
If Vartype(m.toRepository) == "O"
This.oRepository = m.toRepository
EndIf
EndProc
*========================================================================================
* Dispatches any request to either the current mock object, a new mock object or the
* test instance of a mock object.
*========================================================================================
Procedure This_Access (tcMember)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcMember) == "C"
*--------------------------------------------------------------------------------------
* Within the Accessor we only reer to the physical object itself.
*--------------------------------------------------------------------------------------
If Vartype(foxMock__Accessor) == "L"
Return This
Else
Private foxMock__Accessor
foxMock__Accessor = .T.
EndIf
*--------------------------------------------------------------------------------------
* Prepare parameters
*--------------------------------------------------------------------------------------
Local lcMember
lcMember = Alltrim (Lower (m.tcMember))
*--------------------------------------------------------------------------------------
* Which mode do we operate in now?
*
* - object has been created with CreateObject: This is the base module. Definition mode
* main
* - object has been created by new: Definition mode
* - is being called within the same line as corresponding New: definition mode
* - is being called with mock.Extend(obj). -> definition mode on obj
* - otherwise we are in test mode
* - chaining
*--------------------------------------------------------------------------------------
Local loImplementation
If This.InDefinitionMode()
loImplementation = This.PrepareForCommand (m.lcMember)
Else
loImplementation = This.PrepareForTest (m.lcMember)
EndIf
*--------------------------------------------------------------------------------------
* Make sure VFP keeps a reference long enough around to actually access the member.
*--------------------------------------------------------------------------------------
This.oKeepAlive = m.loImplementation
Return m.loImplementation
*========================================================================================
* This object has multiple interfaces. It can be called when it's being tested using
* whatever property or methods have been defined, but it also can be programmed using
* a fluent interface. The following two lines illustrate this:
*
* x = mock.New ;
* .Property("test").Is("Hello")
* ? x.Test
*
*========================================================================================
Hidden Procedure InDefinitionMode
Local lcCaller, llInDefinitionMode
If Empty(This.cDefinitionLine)
llInDefinitionMode = .T.
Else
lcCaller = This.GetCaller (2)
llInDefinitionMode = (m.lcCaller == This.cDefinitionLine)
EndIf
Return m.llInDefinitionMode
*========================================================================================
* We expect a certain property to be accessed next. Make sure we forward the call
* accordingly.
*========================================================================================
Procedure PrepareForCommand (tcMember)
*--------------------------------------------------------------------------------------
* Assertion
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcMember) == "C"
Assert Lower(m.tcMember) == m.tcMember
*--------------------------------------------------------------------------------------
* Test for every possible command and dispatch the appropriate implementation
*--------------------------------------------------------------------------------------
Local loImplementation
Do case
Case m.tcMember == "verifyallexpectations"
loImplementation = This
Case m.tcMember == "add"
loImplementation = This
Case m.tcMember == "cleanup"
loImplementation = This
Case m.tcMember == "new"
loImplementation = CreateObject ("foxMock_New", Null, This.GetRepository ())
Case m.tcMember == "property"
loImplementation = CreateObject ("foxMock_Property", This.GetMaster ())
Case m.tcMember == "is"
loImplementation = CreateObject ("foxMock_Is", This.GetMaster ())
Case m.tcMember == "isobject"
loImplementation = CreateObject ("foxMock_IsObject", This.GetMaster ())
Case m.tcMember == "expect"
loImplementation = CreateObject ("foxMock_Expect", This.GetMaster ())
Case m.tcMember == "callto" or m.tcMember == "method"
loImplementation = CreateObject ("foxMock_CallTo", This.GetMaster ())
Case m.tcMember == "return" or m.tcMember == "returns"
loImplementation = CreateObject ("foxMock_Return", This.GetMaster ())
Case m.tcMember == "asobject"
loImplementation = CreateObject ("foxMock_AsObject", This.GetMaster ())
Case m.tcMember == "returnobject" or m.tcMember == "returnsobject"
loImplementation = CreateObject ("foxMock_ReturnObject", This.GetMaster ())
Case m.tcMember == "when"
loImplementation = CreateObject ("foxMock_When", This.GetMaster ())
Case m.tcMember == "then"
loImplementation = CreateObject ("foxMock_then", This.GetMaster ())
Case m.tcMember == "fail"
loImplementation = CreateObject ("foxMock_Fail", This.GetMaster ())
Case m.tcMember == "scatter"
loImplementation = CreateObject ("foxMock_Scatter", This.GetMaster ())
Case m.tcMember == "bindable"
loImplementation = CreateObject ("foxMock_Bindable", This.GetMaster ())
Case m.tcMember == "reference"
loImplementation = CreateObject ("foxMock_reference", This.GetMaster ())
Otherwise
Assert .F. Message "Unknown operation " + m.tcMember
loImplementation = NULL
EndCase
Return m.loImplementation
*========================================================================================
* Returns the repository object, which is the very first object created by the user.
*========================================================================================
Procedure GetRepository
Local loMaster
If IsNull(This.oRepository)
If IsNull(This.oMaster)
Return This
Else
Return This.oMaster.GetRepository()
EndIf
Else
Return This.oRepository
EndIf
EndProc
*========================================================================================
* Returns the master object implementation
*========================================================================================
Procedure GetMaster
If IsNull(This.oMaster)
Return This
Else
Return This.oMaster
EndIf
EndProc
*========================================================================================
* Return an object with the requested object and the current operation
*========================================================================================
Procedure PrepareForTest (tcMember)
*--------------------------------------------------------------------------------------
* Assertion
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcMember) == "C"
Assert Lower(m.tcMember) == m.tcMember
*--------------------------------------------------------------------------------------
* Find member for current operation among the registered members.
*--------------------------------------------------------------------------------------
Local lnIndex
lnIndex = This.Members.GetKey(m.tcMember)
If Empty(This.cFoundation)
Assert m.lnIndex > 0 Message "Undefined mocked member '"+m.tcMember+"'"
EndIf
*--------------------------------------------------------------------------------------
* We ask the member definition to provide us with an implementation
*--------------------------------------------------------------------------------------
Local loImplementation
If m.lnIndex == 0
loImplementation = This.GetFoundationMaster ()
Else
loImplementation = This.Members[m.lnIndex].GetImplementation()
EndIf
*--------------------------------------------------------------------------------------
* Register the temporary file that we created.
*--------------------------------------------------------------------------------------
This.RegisterTempName (loImplementation.Class)
Return m.loImplementation
*========================================================================================
* Returns a class that implements the basic interface for FoxMock as well as the
* foundation class. Technially it's a sub class of the foundation class and therefore
* has the same runtime requirements. Because most classes aren't stateless, we keep a
* single copy around.
*========================================================================================
Procedure GetFoundationMaster
Local loFoundationDefinition
If IsNull(This.oFoundation)
loFoundationDefinition = CreateObject("mockFoundationDefinition")
loFoundationDefinition.cName = This.cFoundation
This.oFoundation = loFoundationDefinition.GetImplementation (This)
EndIf
Return This.oFoundation
*========================================================================================
* Adds a property or a method to the definition and makes it the current member. Any
* modifier operates on that member then.
*========================================================================================
Procedure RegisterMember (toMember)
Local lcName
lcName = m.toMember.cName
If This.Members.GetKey(m.lcName) == 0
This.Members.Add (m.toMember, m.lcName)
Else
This.Members[m.lcName].Reset()
EndIf
This.ExpectationHandledBy (This.Members[m.lcName])
This.cActiveMember = m.lcName
EndProc
*========================================================================================
* Returns a reference to the active property definition
*========================================================================================
Procedure GetActiveMember
Return This.Members[This.cActiveMember]
*========================================================================================
* Returns .T. when the currently active member is a property
*========================================================================================
Procedure PropertyIsActive
Return This.ActiveMemberIs ("mockPropertyDefinition")
*========================================================================================
* Returns .T. when the currently active member is a method
*========================================================================================
Procedure MethodIsActive
Return This.ActiveMemberIs ("mockMethodDefinition")
*========================================================================================
* Returns .T. when the currently active member is a scatter operation
*========================================================================================
Procedure ScatterIsActive
Return This.ActiveMemberIs ("mockScatterDefinition")
*========================================================================================
* Returns .T. when the currently active member is the passed type
*========================================================================================
Hidden Procedure ActiveMemberIs (tcClass)
Local loMember, llIsClass
loMember = This.GetActiveMember()
If Lower(m.loMember.Class) == Lower(m.tcClass)
llIsClass = .T.
Else
llIsClass = .F.
EndIf
Return m.llIsClass
*========================================================================================
* Remember from which line we have been called. This is the definition code line.
*========================================================================================
Procedure SetDefinitionLine (tcDefiniton)
Assert Vartype(m.tcDefiniton) == "C"
This.cDefinitionLine = m.tcDefiniton
EndProc
*========================================================================================
* Returns the caller line
*========================================================================================
Procedure GetCaller (m.tnOffset)
*--------------------------------------------------------------------------------------
* Assertion
*--------------------------------------------------------------------------------------
Assert Vartype(m.tnOffset) == "N"
Assert Program(-1)-m.tnOffset-1 > 0
*--------------------------------------------------------------------------------------
* Program and line number uniquely identifiers a caller
*--------------------------------------------------------------------------------------
Local laStack[1], lcDefiniton, lnLevel
AStackInfo(laStack)
lnLevel = Program(-1) -1 - m.tnOffset
lcDefiniton = laStack[m.lnLevel,2] + ":" + Transform(laStack[lnLevel,5])
Return m.lcDefiniton
*========================================================================================
* Registers a new expectation in the repository and makes it the current expectation in
* the master object.
*========================================================================================
Procedure NewExpectation (toExpectation)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.toExpectation) == "O"
*--------------------------------------------------------------------------------------
* Make it the current expectation in the master object
*--------------------------------------------------------------------------------------
Local loMaster
loMaster = This.GetMaster()
loMaster.oExpectation = m.toExpectation
*--------------------------------------------------------------------------------------
* All expectations are kept track of in a single location
*--------------------------------------------------------------------------------------
Local loRepository
loRepository = This.GetRepository()
loRepository.Expectations.Add (m.toExpectation)
EndProc
*========================================================================================
* If there's a current expectation, we link with the handler
*========================================================================================
Procedure ExpectationHandledBy (toHandler)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.toHandler) == "O"
*--------------------------------------------------------------------------------------
* This expectation is now assigned to a handler.
*--------------------------------------------------------------------------------------
If Vartype(This.oExpectation) == "O"
This.oExpectation.HandledBy = m.toHandler
This.oExpectation = NULL
toHandler.lExpectation = .T.
Else
toHandler.lExpectation = .F.
EndIf
EndProc
*========================================================================================
* Verfifies that all expectations have been met.
*========================================================================================
Procedure VerifyAllExpectations
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Query the handler for each registered expectation
*--------------------------------------------------------------------------------------
Local loExpectation
For each loExpectation in This.Expectations FoxObject
If not loExpectation.HandledBy.VerifyExpectation()
Error 2005, "Expectation failed for " + loExpectation.HandledBy.cName
EndIf
EndFor
EndProc
*========================================================================================
* Adds a temporary file name
*========================================================================================
Procedure RegisterTempName (tcName)
Local loRepository
loRepository = This.GetRepository()
loRepository.cTempFiles = loRepository.cTempFiles + m.tcName + Chr(13) + Chr(10)
EndProc
*========================================================================================
* Delete all temporarily created files
*========================================================================================
Procedure CleanUp
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Remove objects from repository
*--------------------------------------------------------------------------------------
Local loRepository, lnItem
loRepository = This.GetRepository()
For lnItem = loRepository.Count to 1 step -1
loRepository.Remove (m.lnItem)
EndFor
*--------------------------------------------------------------------------------------
* Remove class and temporary files. We can remove all but the last test object
*--------------------------------------------------------------------------------------
Local laFiles[1], lnFile, lcName
For lnFile = 1 to ALines (laFiles, This.cTempFiles, 4)
lcName = laFiles[m.lnFile]
Clear Class (m.lcName)
Try
Erase (Addbs (GetEnv ("TEMP"))+m.lcName+".fxp")
Catch
EndTry
Erase (Addbs (GetEnv ("TEMP"))+m.lcName+".prg")
Erase (Addbs (GetEnv ("TEMP"))+m.lcName+".err")
EndFor
EndProc
*========================================================================================
* Destroy clean up all files automatically
*========================================================================================
Procedure Destroy
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Remove reference to foundation class.
*--------------------------------------------------------------------------------------
Local loMaster
loMaster = This.GetMaster ()
loMaster.oFoundation = null
*--------------------------------------------------------------------------------------
* Clean up temporary files when the original mock object is released.
*--------------------------------------------------------------------------------------
If This.oRepository = This
This.CleanUp ()
EndIf
EndProc
EndDefine
*========================================================================================
* The new command returns a new mockup object. This new object later becomes the master.
*========================================================================================
Define Class foxMock_New as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Dimension New[1]
*========================================================================================
* Returns a new mockup object
*========================================================================================
Procedure New_Access (tcClass)
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Create a new mockup object
*--------------------------------------------------------------------------------------
Local loMaster
loMaster = CreateObject ("foxMock", NULL, This.GetRepository())
*--------------------------------------------------------------------------------------
* Everything called from the same line as the New line is considered to be a
* definition. Everything else uses the mockup.
*--------------------------------------------------------------------------------------
loMaster.SetDefinitionLine (m.loMaster.GetCaller( 1))
If not Empty(m.tcClass)
loMaster.cFoundation = m.tcClass
EndIf
Return m.loMaster
EndDefine
*========================================================================================
* Property adds a new property to the master object
*========================================================================================
Define Class foxMock_Property as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Dimension Property[1]
*========================================================================================
* Registers a new property for the mockup
*========================================================================================
Procedure Property_Access (tcProperty)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcProperty) == "C"
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* We only use lower case names internally
*--------------------------------------------------------------------------------------
Local lcProperty
lcProperty = Lower (m.tcProperty)
*--------------------------------------------------------------------------------------
* The property object keeps track of a few pieces we need to know about every simulated
* property.
*--------------------------------------------------------------------------------------
Local loProperty
loProperty = CreateObject("mockPropertyDefinition")
loProperty.cName = m.lcProperty
*--------------------------------------------------------------------------------------
* Register the new property in the master object
*--------------------------------------------------------------------------------------
Local loMaster
loMaster = This.GetMaster()
loMaster.RegisterMember (m.loProperty)
Return m.loMaster
EndDefine
*========================================================================================
* Add properties from a record using SCATTER
*========================================================================================
Define Class foxMock_Scatter as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Dimension Scatter[1]
*========================================================================================
* Registers a set of new properties for the mockup
*========================================================================================
Procedure Scatter_Access (tcDefinition)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcDefinition) == "C"
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Returns the specified record
*--------------------------------------------------------------------------------------
Local loRecord, loMaster
loMaster = This.GetMaster()
loRecord = This.GetRecord (&tcDefinition)
*--------------------------------------------------------------------------------------
* Register each member as a property with a value
*--------------------------------------------------------------------------------------
Local laMembers[1], lnMember, lcName
If Vartype(m.loRecord) == "O"
For lnMember = 1 to AMembers(laMembers, m.loRecord)
lcName = Lower (laMembers[m.lnMember])
This.AddMember (m.loMaster, m.lcName, GetPem (m.loRecord, m.lcName))
EndFor
EndIf
Return m.loMaster
*========================================================================================
* Reads the specified record
*========================================================================================
Procedure GetRecord (tcAlias, tcLocate)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcAlias) == "C"
Assert Vartype(m.tcLocate) $ "CL"
*--------------------------------------------------------------------------------------
* Save environment
*--------------------------------------------------------------------------------------
Local lnSelect, lnRecNo
lnSelect = Select()
If Used(m.tcAlias)
Select (m.tcAlias)
lnRecNo = Recno()
Else
Return null
EndIf
*--------------------------------------------------------------------------------------
* Load the desired record
*--------------------------------------------------------------------------------------
Local loRecord
If not Empty (m.tcLocate)
Locate &tcLocate
EndIf
Scatter name loRecord Memo
*--------------------------------------------------------------------------------------
* Restore environment
*--------------------------------------------------------------------------------------
If not Empty (m.lnRecNo)
Locate RECORD m.lnRecNo
EndIf
Select (m.lnSelect)
Return m.loRecord
*========================================================================================
* Adds a property member
*========================================================================================
Procedure AddMember (toMaster, tcName, tuValue)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.toMaster) == "O"
Assert Vartype(m.tcName) == "C"
*--------------------------------------------------------------------------------------
* The property object keeps track of a few pieces we need to know about every simulated
* property.
*--------------------------------------------------------------------------------------
Local loProperty
loProperty = CreateObject("mockPropertyDefinition")
loProperty.cName = m.tcName
loProperty.uValue = m.tuValue
*--------------------------------------------------------------------------------------
* Register the new property in the master object
*--------------------------------------------------------------------------------------
toMaster.RegisterMember (m.loProperty)
EndProc
EndDefine
*========================================================================================
* The Is operation sets the value for the current property
*========================================================================================
Define Class foxMock_Is as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Dimension Is[1]
*========================================================================================
* Stores the value for the active property. Because VFP can only handle numbers and
* strings in an access method, we pass the expression instead of the value.
*========================================================================================
Procedure Is_Access (tcValue)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcValue) == "C"
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Store the value
*--------------------------------------------------------------------------------------
Local loMaster, loProperty, loMethod
loMaster = This.GetMaster()
Do case
Case loMaster.PropertyIsActive()
loProperty = loMaster.GetActiveMember()
loProperty.uValue = Evaluate(m.tcValue)
Case loMaster.MethodIsActive()
loMethod = loMaster.GetActiveMember()
If loMethod.IsReferenceActive()
loMethod.SetReferenceValue (Evaluate(m.tcValue))
Else
Assert .F. Message "operation 'Is' only valid for properties and references"
EndIf
Otherwise
Assert .F. Message "operation 'Is' only valid for properties and references"
EndCase
Return m.loMaster
EndDefine
*========================================================================================
* The IsObject operation sets the object value for the current property
*========================================================================================
Define Class foxMock_IsObject as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never use this.
*--------------------------------------------------------------------------------------
Dimension IsObject[1]
*========================================================================================
* Stores the value for the active property. We store the object key rather then the
* actual object.
*========================================================================================
Procedure IsObject_Access (tcId)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcId) == "C"
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Store the value
*--------------------------------------------------------------------------------------
Local loMaster, loProperty, loObj, loRepository, loMethod
loMaster = This.GetMaster()
Do case
Case loMaster.PropertyIsActive()
loProperty = loMaster.GetActiveMember()
loRepository = This.GetRepository()
loObj = loRepository.Item(m.tcId)
Assert Vartype(m.loObj) == "O"
loProperty.uValue = m.loObj
Case loMaster.MethodIsActive()
loMethod = loMaster.GetActiveMember()
If m.loMethod.IsReferenceActive()
loRepository = This.GetRepository()
loObj = loRepository.Item(m.tcId)
Assert Vartype(m.loObj) == "O"
loMethod.SetReferenceValue (m.loObj)
Else
Assert .F. Message "operation 'IsObject' only valid for properties and references"
EndIf
Otherwise
Assert .F. Message "operation 'IsObject' only valid for properties and references"
EndCase
Return m.loMaster
EndDefine
*========================================================================================
* Indicates that the following operation must occur in order to pass the test.
*========================================================================================
Define Class foxMock_Expect as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Expect = null
*========================================================================================
* We set the expect flag in the master object.
*========================================================================================
Procedure Expect_Access
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Every expectation is handled by an implementation that is created by a subsequent
* operation. For instance, a CallTo operation expects a method to be called. This is
* handled by the method definition.
*--------------------------------------------------------------------------------------
Local loExpectation
loExpectation = CreateObject("Empty")
AddProperty (loExpectation, "HandledBy", NULL)
*--------------------------------------------------------------------------------------
* Subsequent operations query the Expect flag
*--------------------------------------------------------------------------------------
Local loMaster
loMaster = This.GetMaster()
loMaster.NewExpectation (m.loExpectation)
Return m.loMaster
EndDefine
*========================================================================================
* The CallTo operation adds a simulated method to the master object
*========================================================================================
Define Class foxMock_CallTo as foxMock
*--------------------------------------------------------------------------------------
* Just so that VFP can actually find the definition. We never us this.
*--------------------------------------------------------------------------------------
Dimension CallTo[1]
Dimension Method[1]
*========================================================================================
* Registers a new method for the mockup
*========================================================================================
Procedure CallTo_Access (tcMethod)
*--------------------------------------------------------------------------------------
* Assertions
*--------------------------------------------------------------------------------------
Assert Vartype(m.tcMethod) == "C"
*--------------------------------------------------------------------------------------
* The following code operates on the master object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* We only use lower case names internally
*--------------------------------------------------------------------------------------
Local lcMethod
lcMethod = Lower (m.tcMethod)
*--------------------------------------------------------------------------------------
* The method object keeps track of a few pieces we need to know about every simulated
* method.
*--------------------------------------------------------------------------------------
Local loMethod, loMaster
loMaster = This.GetMaster()
loMethod = CreateObject("mockMethodDefinition")
loMethod.cName = m.lcMethod
loMaster.RegisterMember (m.loMethod)
Return m.loMaster
*========================================================================================
* Define aliases for this operation
*========================================================================================
Procedure Method_Access (tcMethod)
Private foxMock__Accessor
foxMock__Accessor = .T.
Return This.CallTo_Access (m.tcMethod)
EndDefine
*========================================================================================
* The Return operation specifies the return value for a method
*========================================================================================
Define Class foxMock_ReturnOperation as foxMock
*========================================================================================
* Stores the return value for the active method.
*========================================================================================
Procedure Store (tuValue)
*--------------------------------------------------------------------------------------
* The following code operates on the object directly
*--------------------------------------------------------------------------------------
Private foxMock__Accessor
foxMock__Accessor = .T.
*--------------------------------------------------------------------------------------
* Keep the value
*--------------------------------------------------------------------------------------
Local loMaster, loMethod
loMaster = This.GetMaster()
If loMaster.MethodIsActive()
loMethod = loMaster.GetActiveMember()
loMethod.SetReturnValue(m.tuValue)
Else
Assert .F. Message "operation of the 'Return'-family only valid for methods"
EndIf
Return m.loMaster
EndDefine
*========================================================================================
* The Return operation expects the return value as an expression
*========================================================================================