// CLASS 기능 전수 테스트 // TBrowse 포팅에 필요한 모든 CLASS 기능 CLASS Counter DATA nValue INIT 0 DATA nStep INIT 1 DATA cName INIT "default" METHOD New(cName, nStep) METHOD Inc() METHOD Dec() METHOD GetValue() METHOD SetValue(n) METHOD Reset() METHOD ToString() ENDCLASS METHOD New(cName, nStep) CLASS Counter ::cName := cName IF nStep != NIL ::nStep := nStep ENDIF RETURN Self METHOD Inc() CLASS Counter ::nValue += ::nStep RETURN Self METHOD Dec() CLASS Counter ::nValue -= ::nStep RETURN Self METHOD GetValue() CLASS Counter RETURN ::nValue METHOD SetValue(n) CLASS Counter ::nValue := n RETURN Self METHOD Reset() CLASS Counter ::nValue := 0 RETURN Self METHOD ToString() CLASS Counter RETURN ::cName + "=" + Str(::nValue) // Inheritance test CLASS StepCounter INHERIT FROM Counter DATA nMaxValue INIT 100 METHOD Inc() METHOD IsMax() ENDCLASS METHOD Inc() CLASS StepCounter IF ::nValue + ::nStep <= ::nMaxValue ::nValue += ::nStep ENDIF RETURN Self METHOD IsMax() CLASS StepCounter RETURN ::nValue >= ::nMaxValue FUNCTION Main() LOCAL o, o2, nPass := 0 ? "=== CLASS Full Test ===" ? "" // 1. Basic construction ? "--- 1. Construction ---" o := Counter():New("test", 5) nPass += Assert("New name", o:cName, "test") nPass += Assert("New step", o:nStep, 5) nPass += Assert("Init value", o:nValue, 0) // 2. Method calls ? "--- 2. Methods ---" o:Inc() nPass += Assert("Inc once", o:GetValue(), 5) o:Inc() o:Inc() nPass += Assert("Inc 3x", o:GetValue(), 15) o:Dec() nPass += Assert("Dec", o:GetValue(), 10) // 3. Method chaining (RETURN Self) ? "--- 3. Chaining ---" o:Reset():Inc():Inc() nPass += Assert("Chain reset+inc+inc", o:GetValue(), 10) // 4. SetValue + ToString ? "--- 4. Setters ---" o:SetValue(42) nPass += Assert("SetValue", o:GetValue(), 42) nPass += Assert("ToString", o:ToString(), "test=42") // 5. Multiple instances ? "--- 5. Multiple instances ---" o2 := Counter():New("other", 10) o2:Inc():Inc():Inc() nPass += Assert("Instance 2", o2:GetValue(), 30) nPass += Assert("Instance 1 unchanged", o:GetValue(), 42) // 6. Inheritance ? "--- 6. Inheritance ---" LOCAL oStep := StepCounter():New("step", 25) oStep:nMaxValue := 50 oStep:Inc() nPass += Assert("StepCounter inc", oStep:GetValue(), 25) oStep:Inc() nPass += Assert("StepCounter inc 2", oStep:GetValue(), 50) oStep:Inc() nPass += Assert("StepCounter max", oStep:GetValue(), 50) nPass += Assert("IsMax", oStep:IsMax(), .T.) // 7. Inherited method nPass += Assert("Inherited ToString", oStep:ToString(), "step=50") // 8. Field access as getter ? "--- 7. Field access ---" nPass += Assert("Field getter", o:cName, "test") nPass += Assert("Field getter 2", oStep:nMaxValue, 50) // Summary ? "" ? "========================" ? " CLASS PASS:", nPass ? "========================" RETURN NIL FUNCTION Assert(cDesc, xGot, xExpected) IF ValType(xGot) = ValType(xExpected) .AND. xGot = xExpected RETURN 1 ENDIF ? " FAIL:", cDesc ? " Got:", xGot ? " Exp:", xExpected RETURN 0