@@ -65,15 +65,56 @@ private static CoderEditorApp NewApp(DocumentStore store, EditorSettings? settin
6565 settings ?? new EditorSettings ( ) ) ;
6666
6767 /// <summary>
68- /// Tests that a fresh editor opens with something to attach to rather than a blank canvas.
68+ /// Tests that a document containing an assignment survives being written and read back.
69+ /// </summary>
70+ /// <remarks>
71+ /// AssignmentStatement's deserialization constructor built its placeholder target through the
72+ /// VariableReference overload that rejects an empty name, so every load of a document holding one
73+ /// threw before it could be overwritten. The default document has an assignment in it, so this is
74+ /// the path a user takes by saving and reopening what the editor gave them.
75+ /// </remarks>
76+ [ TestMethod ]
77+ public void Document_WithAnAssignment_RoundTripsThroughTheFileSystem ( )
78+ {
79+ DocumentStore store = NewStore ( ) ;
80+ CoderEditorApp app = NewApp ( store ) ;
81+
82+ string path = PathIn ( "assigning" ) ;
83+ Assert . IsTrue ( app . Save ( path ) , app . Status ) ;
84+
85+ CoderEditorApp reopened = NewApp ( store ) ;
86+ Assert . IsTrue ( reopened . Open ( path ) , reopened . Status ) ;
87+
88+ ClassDeclaration reopenedRoot = ( ClassDeclaration ) reopened . Editor . Graph . Root ;
89+ Assert . IsTrue (
90+ reopenedRoot . Members . OfType < FunctionDeclaration > ( ) . SelectMany ( m => m . Body ) . OfType < AssignmentStatement > ( ) . Any ( ) ,
91+ "the reopened document should still hold its assignment" ) ;
92+ }
93+
94+ /// <summary>
95+ /// Tests that a fresh editor opens with something to read rather than a blank canvas.
6996 /// </summary>
7097 [ TestMethod ]
71- public void NewDocument_IsAFunctionWithSomethingToAttachTo ( )
98+ public void NewDocument_IsAClassWithFieldsAndMethodsThatDoSomething ( )
7299 {
73- FunctionDeclaration document = CoderEditorApp . NewDocument ( ) ;
100+ ClassDeclaration document = CoderEditorApp . NewDocument ( ) ;
101+
102+ Assert . AreEqual ( "Counter" , document . Name ) ;
103+
104+ List < VariableDeclaration > fields = [ .. document . Members . OfType < VariableDeclaration > ( ) ] ;
105+ Assert . AreEqual ( 2 , fields . Count , "the class should carry a couple of fields" ) ;
106+ Assert . IsTrue ( fields . TrueForAll ( f => f . InitialValue is not null ) , "each field should be initialised" ) ;
74107
75- Assert . AreEqual ( "newFunction" , document . Name ) ;
76- Assert . AreEqual ( 1 , document . Parameters . Count ) ;
108+ List < FunctionDeclaration > methods = [ .. document . Members . OfType < FunctionDeclaration > ( ) ] ;
109+ Assert . AreEqual ( 2 , methods . Count , "the class should carry a couple of methods" ) ;
110+ Assert . IsTrue ( methods . TrueForAll ( m => m . Body . Count > 0 ) , "each method should have a body" ) ;
111+
112+ Assert . IsTrue (
113+ methods . SelectMany ( m => m . Body ) . OfType < AssignmentStatement > ( ) . Any ( a => a . Value is BinaryExpression ) ,
114+ "a method should assign the result of an expression" ) ;
115+ Assert . IsTrue (
116+ methods . SelectMany ( m => m . Body ) . OfType < VariableDeclaration > ( ) . Any ( v => v . InitialValue is BinaryExpression ) ,
117+ "a method should declare a local from an expression" ) ;
77118 }
78119
79120 /// <summary>
@@ -92,8 +133,8 @@ public void Document_RoundTripsThroughTheFileSystem()
92133 CoderEditorApp reopened = NewApp ( store ) ;
93134 Assert . IsTrue ( reopened . Open ( path ) , reopened . Status ) ;
94135
95- Assert . IsInstanceOfType < FunctionDeclaration > ( reopened . Editor . Graph . Root ) ;
96- Assert . AreEqual ( "newFunction " , ( ( FunctionDeclaration ) reopened . Editor . Graph . Root ) . Name ) ;
136+ Assert . IsInstanceOfType < ClassDeclaration > ( reopened . Editor . Graph . Root ) ;
137+ Assert . AreEqual ( "Counter " , ( ( ClassDeclaration ) reopened . Editor . Graph . Root ) . Name ) ;
97138 Assert . AreEqual ( path , reopened . DocumentPath ) ;
98139 }
99140
@@ -259,11 +300,11 @@ public void Preview_GeneratesInTheSelectedLanguage()
259300 CoderEditorApp app = NewApp ( store , settings ) ;
260301
261302 app . Regenerate ( ) ;
262- StringAssert . Contains ( app . GeneratedCode , "public void newFunction " , StringComparison . Ordinal ) ;
303+ StringAssert . Contains ( app . GeneratedCode , "public class Counter " , StringComparison . Ordinal ) ;
263304
264305 settings . PreviewLanguageId = "python" ;
265306 app . Regenerate ( ) ;
266- StringAssert . Contains ( app . GeneratedCode , "def newFunction " , StringComparison . Ordinal ) ;
307+ StringAssert . Contains ( app . GeneratedCode , "def Add(self, amount: int) " , StringComparison . Ordinal ) ;
267308 }
268309
269310 /// <summary>
@@ -367,7 +408,7 @@ public void Export_WritesTheGeneratedCodeBesideTheDocument()
367408
368409 Assert . IsNotNull ( written ) ;
369410 Assert . AreEqual ( Path . Combine ( root , "greeting.cs" ) , written ) ;
370- StringAssert . Contains ( File . ReadAllText ( written ) , "public void newFunction (int value )" , StringComparison . Ordinal ) ;
411+ StringAssert . Contains ( File . ReadAllText ( written ) , "public int Add (int amount )" , StringComparison . Ordinal ) ;
371412 StringAssert . Contains ( app . Status , "Wrote" , StringComparison . Ordinal ) ;
372413 }
373414
@@ -521,7 +562,7 @@ public void CodePane_ListsProblemsInsteadOfGeneratingWhileIncomplete()
521562 CoderEditorApp app = NewApp ( store ) ;
522563
523564 // An operand nobody has filled in yet, which is what Validate reports.
524- FunctionDeclaration document = CoderEditorApp . NewDocument ( ) ;
565+ FunctionDeclaration document = new ( "incomplete" ) { ReturnType = "int" } ;
525566 document . Body . Add ( new ReturnStatement (
526567 new BinaryExpression ( AstSchema . Unfilled ( ) , BinaryOperator . Add , AstSchema . Unfilled ( ) ) ) ) ;
527568 Assert . IsTrue ( app . Open ( WriteDocument ( store , document , PathIn ( "incomplete" ) ) ) , app . Status ) ;
0 commit comments