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 m_Runtime = runtime.Clone()
45 .UseApi(new BadNUnitApi())
46 .UseApi(api);
47 }
48
49
56 public void Register(bool optimizeFolding, bool optimizeSubstitution, params string[] files)
57 {
58 foreach (string file in files)
59 {
60 SetupStage(file, optimizeFolding, optimizeSubstitution);
61 }
62 }
63
68 public BadUnitTestContext CreateContext(string workingDir)
69 {
70 return new BadUnitTestContext(workingDir, m_Cases.ToList(), m_Setup.ToList(), m_Teardown.ToList(), m_Runtime);
71 }
72
80 public void AddTest(BadFunction function, BadObject testName, bool allowCompile = true)
81 {
82 string? name = null;
83
84 if (testName != BadObject.Null)
85 {
86 name = (testName as IBadString)?.Value ?? throw new InvalidOperationException("Test name must be a string");
87 }
88
89 m_Cases.Add(new BadNUnitTestCase(function, name, allowCompile));
90 }
91
96 public void AddSetup(BadFunction function)
97 {
98 m_Setup.Add(function);
99 }
100
101
106 public void AddTeardown(BadFunction function)
107 {
108 m_Teardown.Add(function);
109 }
110
114 public void Reset()
115 {
116 m_Teardown.Clear();
117 m_Setup.Clear();
118 m_Cases.Clear();
119 }
120
121
129 private void SetupStage(string file, bool optimizeFolding = false, bool optimizeSubstitution = false)
130 {
131 //Load expressions
132 IEnumerable<BadExpression> expressions = BadSourceParser.Create(file, BadFileSystem.ReadAllText(file)).Parse();
133
134 if (optimizeFolding)
135 {
136 expressions = BadConstantFoldingOptimizer.Optimize(expressions);
137 }
138
139 if (optimizeSubstitution)
140 {
141 expressions = BadConstantSubstitutionOptimizer.Optimize(expressions);
142 }
143
144 m_Runtime.Execute(expressions, Path.GetDirectoryName(BadFileSystem.Instance.GetFullPath(file)) ?? "/");
145 }
146}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
BadRuntime Clone()
Clone this Runtime.
Definition BadRuntime.cs:96
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