< Summary

Class:DCL.Mocked
Assembly:ABConverter
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/AssetDatabase.cs
/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/Directory.cs
/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/File.cs
/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/WebRequest.cs
Covered lines:0
Uncovered lines:123
Coverable lines:123
Total lines:307
Line coverage:0% (0 of 123)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetDatabase()0%2100%
AssetDatabase(...)0%2100%
Refresh(...)0%12300%
SaveAssets()0%2100%
ImportAsset(...)0%12300%
DeleteAsset(...)0%6200%
MoveAsset(...)0%6200%
ReleaseCachedFileHandles()0%2100%
LoadAssetAtPath[T](...)0%12300%
GetAssetPath(...)0%2100%
AssetPathToGUID(...)0%2100%
GetTextMetaFilePathFromAssetPath(...)0%2100%
GetImporterAtPath(...)0%2100%
Directory()0%2100%
CreateDirectory(...)0%6200%
InitializeDirectory(...)0%2100%
Delete(...)0%6200%
Exists(...)0%2100%
File()0%2100%
File()0%2100%
Delete(...)0%6200%
Exists(...)0%2100%
Copy(...)0%6200%
Move(...)0%6200%
ReadAllText(...)0%6200%
WriteAllText(...)0%12300%
WriteAllBytes(...)0%20400%
OpenRead(...)0%6200%
GenerateStreamFromString(...)0%2100%
GetText()0%2100%
GetData()0%2100%
WebRequest()0%2100%
Get(...)0%6200%
GetAsync(...)0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/AssetDatabase.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using DCL;
 4using UnityEditor;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public sealed partial class Mocked
 10    {
 11        //TODO(Brian): Evaluate if we can use mocking library to replace this mock
 12        public class AssetDatabase : IAssetDatabase
 13        {
 014            private static Logger logger = new Logger("Mocked.AssetDatabase") { verboseEnabled = false };
 15
 016            public HashSet<string> importedAssets = new HashSet<string>();
 017            public HashSet<string> savedAssets = new HashSet<string>();
 18
 19            private IFile file;
 20
 021            public AssetDatabase(IFile file) { this.file = file; }
 22
 23            public void Refresh(ImportAssetOptions options = ImportAssetOptions.Default)
 24            {
 025                logger.Verbose("Refreshing assets...");
 26
 027                foreach (var asset in importedAssets)
 28                {
 029                    string metaPath = Path.ChangeExtension(asset, ".meta");
 30
 031                    if (!file.Exists(metaPath))
 032                        file.Copy(asset, metaPath);
 33                }
 034            }
 35
 36            public void SaveAssets()
 37            {
 038                logger.Verbose("Saving assets...");
 039                savedAssets = new HashSet<string>(importedAssets);
 040            }
 41
 42            public void ImportAsset(string fullPath, ImportAssetOptions options = ImportAssetOptions.Default)
 43            {
 044                if (!file.Exists(fullPath))
 45                {
 046                    logger.Verbose($"Importing asset fail (not exist)...\n({fullPath})");
 047                    return;
 48                }
 49
 050                if (importedAssets.Contains(fullPath))
 51                {
 052                    logger.Verbose($"Importing asset fail (contained)...\n({fullPath})");
 053                    return;
 54                }
 55
 056                file.Copy(fullPath, Path.ChangeExtension(fullPath, ".meta"));
 057                importedAssets.Add(fullPath);
 058                logger.Verbose($"Importing asset...\n({fullPath})");
 059            }
 60
 61            public bool DeleteAsset(string path)
 62            {
 063                if (importedAssets.Contains(path))
 64                {
 065                    logger.Verbose($"Delete asset...\n({path})");
 066                    importedAssets.Remove(path);
 067                    return true;
 68                }
 69
 070                logger.Verbose($"Delete asset... (fail, missing)\n({path})");
 071                return false;
 72            }
 73
 74            public string MoveAsset(string src, string dst)
 75            {
 076                if (importedAssets.Contains(src))
 77                {
 078                    logger.Verbose($"Move asset from {src} to {dst}");
 079                    importedAssets.Remove(src);
 080                    importedAssets.Add(src);
 081                    return "";
 82                }
 83
 084                logger.Verbose($"Move asset from {src} to {dst} (fail)");
 085                return "Error";
 86            }
 87
 088            public void ReleaseCachedFileHandles() { }
 89
 90            public T LoadAssetAtPath<T>(string path) where T : Object
 91            {
 092                logger.Verbose($"LoadAssetAtPath {path}");
 93
 094                if (file.Exists(path))
 95                {
 096                    if (typeof(T) == typeof(Texture2D))
 97                    {
 098                        return Texture2D.whiteTexture as T;
 99                    }
 100
 0101                    return new Object() as T;
 102                }
 103
 0104                return null;
 105            }
 106
 0107            public string GetAssetPath(Object asset) { return ""; }
 108
 0109            public string AssetPathToGUID(string path) { return ""; }
 110
 0111            public string GetTextMetaFilePathFromAssetPath(string path) { return Path.ChangeExtension(path, ".meta"); }
 112
 0113            public AssetImporter GetImporterAtPath(string path) { return null; }
 114        }
 115    }
 116}

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/Directory.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace DCL
 5{
 6    public sealed partial class Mocked
 7    {
 8        //TODO(Brian): Use mocking library to replace this mock
 9        public class Directory : IDirectory
 10        {
 011            public Dictionary<string, string> mockedDirs = new Dictionary<string, string>();
 12
 13            public void CreateDirectory(string path)
 14            {
 015                if (string.IsNullOrEmpty(path))
 016                    throw new Exception("file contents empty!");
 17
 018                mockedDirs.Add(path, path);
 019            }
 20
 021            public void InitializeDirectory(string path, bool deleteIfExists) { Delete(path); }
 22
 23            public void Delete(string path, bool recursive = true)
 24            {
 025                if (Exists(path))
 026                    mockedDirs.Remove(path);
 027            }
 28
 029            public bool Exists(string path) { return mockedDirs.ContainsKey(path); }
 30        }
 31    }
 32}

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/File.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    public sealed partial class Mocked
 9    {
 10        //TODO(Brian): Use mocking library to replace this mock
 11        public class File : IFile
 12        {
 013            private static Logger logger = new Logger("Mocked.File") { verboseEnabled = false };
 014            public Dictionary<string, string> mockedFiles = new Dictionary<string, string>();
 15
 16            public void Delete(string path)
 17            {
 018                logger.Verbose($"Deleting {path}");
 019                if (Exists(path))
 020                    mockedFiles.Remove(path);
 021            }
 22
 23            public bool Exists(string path)
 24            {
 025                bool result = mockedFiles.ContainsKey(path);
 026                logger.Verbose($"Exists? ({path}) == {result}");
 027                return mockedFiles.ContainsKey(path);
 28            }
 29
 30            public void Copy(string srcPath, string dstPath)
 31            {
 032                if (!Exists(srcPath))
 033                    throw new FileNotFoundException($"Not found! ({srcPath})", srcPath);
 34
 035                logger.Verbose($"Copy from {srcPath} to {dstPath}");
 036                mockedFiles.Add(dstPath, mockedFiles[srcPath]);
 037            }
 38
 39            public void Move(string srcPath, string dstPath)
 40            {
 041                if (!Exists(srcPath))
 042                    throw new FileNotFoundException($"Not found! ({srcPath})", srcPath);
 43
 044                logger.Verbose($"Move from {srcPath} to {dstPath}");
 045                Copy(srcPath, dstPath);
 046                Delete(srcPath);
 047            }
 48
 49            public string ReadAllText(string path)
 50            {
 051                if (!Exists(path))
 052                    throw new FileNotFoundException($"Not found! ({path})", path);
 53
 054                logger.Verbose($"Read all text from {path} = {mockedFiles[path]}");
 055                return mockedFiles[path];
 56            }
 57
 58            public void WriteAllText(string path, string text)
 59            {
 060                if (string.IsNullOrEmpty(text))
 061                    throw new Exception("file contents empty!");
 62
 063                logger.Verbose($"WriteAllText to {path} = {text}");
 64
 065                if (!mockedFiles.ContainsKey(path))
 066                    mockedFiles.Add(path, text);
 67                else
 068                    mockedFiles[path] = text;
 069            }
 70
 71            public void WriteAllBytes(string path, byte[] bytes)
 72            {
 073                if (string.IsNullOrEmpty(path))
 074                    throw new Exception("Path empty!");
 75
 076                if (bytes == null)
 077                    throw new Exception("bytes are null!");
 78
 079                string stringBytes = System.Text.Encoding.UTF8.GetString(bytes);
 080                logger.Verbose($"WriteAllText to {path} = {stringBytes}");
 81
 082                if (!mockedFiles.ContainsKey(path))
 083                    mockedFiles.Add(path, stringBytes);
 84                else
 085                    mockedFiles[path] = stringBytes;
 086            }
 87
 88            public Stream OpenRead(string path)
 89            {
 090                if (!Exists(path))
 091                    throw new FileNotFoundException("Not found!", path);
 92
 093                logger.Verbose($"OpenRead path = {path}");
 094                return GenerateStreamFromString(mockedFiles[path]);
 95            }
 96
 97            private static Stream GenerateStreamFromString(string s)
 98            {
 099                var stream = new MemoryStream();
 0100                var writer = new StreamWriter(stream);
 0101                writer.Write(s);
 0102                writer.Flush();
 0103                stream.Position = 0;
 0104                return stream;
 105            }
 106        }
 107    }
 108}

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Wrappers/Implementations/Mock/WebRequest.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Net.Http;
 4using System.Text;
 5using UnityEngine.Networking;
 6
 7namespace DCL
 8{
 9    public sealed partial class Mocked
 10    {
 11        public class DownloadHandler : DownloadHandlerScript
 12        {
 13            public string mockedText;
 14
 015            protected override string GetText() { return mockedText; }
 16
 017            protected override byte[] GetData() { return Encoding.UTF8.GetBytes(mockedText); }
 18        }
 19
 20        public class WebRequest : IWebRequest
 21        {
 22            //This field maps url to url contents.
 023            public Dictionary<string, string> mockedContent = new Dictionary<string, string>();
 24            public float mockedDownloadTime = 0;
 25
 26            public UnityEngine.Networking.DownloadHandler Get(string url)
 27            {
 028                var buffer = new Mocked.DownloadHandler();
 29
 030                if (!mockedContent.ContainsKey(url))
 031                    throw new HttpRequestException($"Mocked 404 -- ({url})");
 32
 033                buffer.mockedText = mockedContent[url];
 034                return buffer;
 35            }
 36
 37            public void GetAsync(string url, Action<UnityEngine.Networking.DownloadHandler> OnCompleted, Action<string> 
 38            {
 039                if (mockedContent.ContainsKey(url))
 40                {
 041                    var buffer = new Mocked.DownloadHandler();
 042                    buffer.mockedText = mockedContent[url];
 043                    OnCompleted?.Invoke(buffer);
 044                    return;
 45                }
 46
 047                OnFail?.Invoke("Url not found!");
 048            }
 49        }
 50    }
 51}