BadScript 2
Loading...
Searching...
No Matches
BadUnitTestContextBuilder.cs
Go to the documentation of this file.
1using BadScript2.IO;
9
11
16{
20 private readonly List<BadNUnitTestCase> m_Cases = new List<BadNUnitTestCase>();
21
25 private readonly BadRuntime m_Runtime;
26
30 private readonly List<BadFunction> m_Setup = new List<BadFunction>();
31
35 private readonly List<BadFunction> m_Teardown = new List<BadFunction>();
36
41 {
43 api.SetContext(this);
44
45 m_Runtime = runtime.Clone()
46 .UseApi(new BadNUnitApi())
47 .UseApi(api);
48 }
49
50
57 public void Register(bool optimizeFolding, bool optimizeSubstitution, params string[] files)
58 {
59 foreach (string file in files)
60 {
61 SetupStage(file, optimizeFolding, optimizeSubstitution);
62 }
63 }
64
69 public BadUnitTestContext CreateContext(string workingDir)
70 {
71 return new BadUnitTestContext(workingDir, m_Cases.ToList(), m_Setup.ToList(), m_Teardown.ToList(), m_Runtime);
72 }
73
81 public void AddTest(BadFunction function, BadObject testName, bool allowCompile = true)
82 {
83 string? name = null;
84
85 if (testName != BadObject.Null)
86 {
87 name = (testName as IBadString)?.Value ?? throw new InvalidOperationException("Test name must be a string");
88 }
89
90 m_Cases.Add(new BadNUnitTestCase(function, name, allowCompile));
91 }
92
97 public void AddSetup(BadFunction function)
98 {
99 m_Setup.Add(function);
100 }
101
102
107 public void AddTeardown(BadFunction function)
108 {
109 m_Teardown.Add(function);
110 }
111
115 public void Reset()
116 {
117 m_Teardown.Clear();
118 m_Setup.Clear();
119 m_Cases.Clear();
120 }
121
122
130 private void SetupStage(string file, bool optimizeFolding = false, bool optimizeSubstitution = false)
131 {
132 //Load expressions
133 IEnumerable<BadExpression> expressions = BadSourceParser.Create(file, BadFileSystem.ReadAllText(file))
134 .Parse();
135
136 if (optimizeFolding)
137 {
138 expressions = BadConstantFoldingOptimizer.Optimize(expressions);
139 }
140
141 if (optimizeSubstitution)
142 {
143 expressions = BadConstantSubstitutionOptimizer.Optimize(expressions);
144 }
145
146 m_Runtime.Execute(expressions, Path.GetDirectoryName(BadFileSystem.Instance.GetFullPath(file)) ?? "/");
147 }
148}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:30
BadRuntime Clone()
Clone this Runtime.
BadRuntimeExecutionResult Execute(IEnumerable< BadExpression > expressions, string workingDirectory)
Executes the specified expressions.
BadRuntime UseApi(BadInteropApi api, bool replace=false)
Adds or Replaces a specified API.
Public interface for the filesystem abstraction of the BadScript Engine.
static string ReadAllText(this IFileSystem fileSystem, string path)
static IFileSystem Instance
File System implementation.
Implements the "NUnit" Api(Console Version)
void SetContext(BadUnitTestContextBuilder console)
Represents a BadScript NUnit Test Case.
void AddSetup(BadFunction function)
Adds a Setup Function to the Test Context.
void SetupStage(string file, bool optimizeFolding=false, bool optimizeSubstitution=false)
Runs the Setup Stage Registering test cases and setup/teardown functions happens here.
void AddTest(BadFunction function, BadObject testName, bool allowCompile=true)
Adds a Test Case to the Test Context.
BadUnitTestContext CreateContext(string workingDir)
Creates a new BadUnitTestContext.
BadUnitTestContextBuilder(BadRuntime runtime)
Constructs a new BadUnitTestContextBuilder.
void AddTeardown(BadFunction function)
Adds a Teardown Function to the Test Context.
readonly List< BadNUnitTestCase > m_Cases
The Test Cases.
readonly List< BadFunction > m_Setup
The Setup Functions.
readonly List< BadFunction > m_Teardown
The Teardown Functions.
void Register(bool optimizeFolding, bool optimizeSubstitution, params string[] files)
Registers one or multiple files to the Test Context.
Implements a BadScript NUnit Test Context.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Contains the Implementation of the Constant Substitution Optimization This optimization replaces expr...
static IEnumerable< BadExpression > Optimize(BadConstantSubstitutionOptimizerScope scope, IEnumerable< BadExpression > expressions)
Substitutes all variables in the expressions with their constant value.
The Parser of the Language. It turns Source Code into an Expression Tree.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a function that can be called from the script.
string GetFullPath(string path)
Returns the full path of the given path.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains IO Implementation for the BadScript2 Runtime.
Contains NUnit Extensions and APIs for the BadScript2 Runtime.
Definition BadNUnitApi.cs:7
Contains the BadScript2 Constant Folding Optimizations.
Contains the BadScript2 Constant Substitution Optimizations.
Contains the Expressions for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10