This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathAnalysisCachingTestBase.cs
More file actions
80 lines (71 loc) · 3.22 KB
/
AnalysisCachingTestBase.cs
File metadata and controls
80 lines (71 loc) · 3.22 KB
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
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Python.Analysis.Analyzer;
using Microsoft.Python.Analysis.Caching.Models;
using Microsoft.Python.Analysis.Caching.Tests.FluentAssertions;
using Microsoft.Python.Analysis.Tests;
using Microsoft.Python.Analysis.Types;
using Newtonsoft.Json;
using TestUtilities;
namespace Microsoft.Python.Analysis.Caching.Tests {
public abstract class AnalysisCachingTestBase : AnalysisTestBase {
protected AnalysisCachingTestBase() {
ModuleFactory.EnableMissingMemberAssertions = true;
}
protected static string ToJson(object model) {
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
using (var jw = new JsonTextWriter(sw)) {
jw.Formatting = Formatting.Indented;
var js = new JsonSerializer();
js.Serialize(jw, model);
}
return sb.ToString();
}
protected string BaselineFilesFolder {
get {
var testAssembly = Assembly.GetExecutingAssembly().GetAssemblyPath();
var outDirectory = Path.GetDirectoryName(testAssembly);
return Path.GetFullPath(Path.Combine(outDirectory, "..", "..", "..", "src", "Caching", "Test", "Files"));
}
}
protected string GetBaselineFileName(string testName, string suffix = null)
=> Path.ChangeExtension(suffix == null
? Path.Combine(BaselineFilesFolder, testName)
: Path.Combine(BaselineFilesFolder, testName + suffix), "json");
internal PythonDbModule CreateDbModule(ModuleModel model, string modulePath) {
var dbModule = new PythonDbModule(model, modulePath, Services);
dbModule.Construct(model);
return dbModule;
}
internal async Task CompareRestoreAsync(ModuleModel model, IPythonModule m, bool recursive = false) {
var analyzer = Services.GetService<IPythonAnalyzer>();
await analyzer.WaitForCompleteAnalysisAsync();
using (var dbModule = CreateDbModule(model, m.FilePath)) {
dbModule.Should().HaveSameMembersAs(m, recursive);
}
}
internal async Task<ModuleModel> GetModelAsync(string code) {
var analysis = await GetAnalysisAsync(code);
var model = ModuleModel.FromAnalysis(analysis, Services, AnalysisCachingLevel.Library);
model.FilePath = null;
return model;
}
}
}