• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zorbathut / dec / 11670896818

04 Nov 2024 07:02PM UTC coverage: 90.556%. Remained the same
11670896818

push

github

zorbathut
Removed redundant namespaces

4564 of 5040 relevant lines covered (90.56%)

191694.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.51
/src/ReaderXmlDec.cs
1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Xml.Linq;
6

7
namespace Dec
8
{
9
    internal class ReaderFileDecXml : ReaderFileDec
10
    {
11
        private XDocument doc;
12
        private string fileIdentifier;
13
        private Recorder.IUserSettings userSettings;
14

15
        public static ReaderFileDecXml Create(TextReader input, string identifier, Recorder.IUserSettings userSettings)
16
        {
12,570✔
17
            XDocument doc = UtilXml.ParseSafely(input);
12,570✔
18
            if (doc == null)
12,570✔
19
            {
60✔
20
                return null;
60✔
21
            }
22

23
            var result = new ReaderFileDecXml();
12,510✔
24
            result.doc = doc;
12,510✔
25
            result.fileIdentifier = identifier;
12,510✔
26
            result.userSettings = userSettings;
12,510✔
27
            return result;
12,510✔
28
        }
12,570✔
29

30
        public override List<ReaderDec> ParseDecs()
31
        {
12,505✔
32
            if (doc.Elements().Count() > 1)
12,505✔
33
            {
×
34
                // This isn't testable, unfortunately; XDocument doesn't even support multiple root elements.
35
                Dbg.Err($"{fileIdentifier}: Found {doc.Elements().Count()} root elements instead of the expected 1");
×
36
            }
×
37

38
            var result = new List<ReaderDec>();
12,505✔
39

40
            foreach (var rootElement in doc.Elements())
62,525✔
41
            {
12,505✔
42
                var rootContext = new InputContext(fileIdentifier, rootElement);
12,505✔
43
                if (rootElement.Name.LocalName != "Decs")
12,505✔
44
                {
20✔
45
                    Dbg.Wrn($"{rootContext}: Found root element with name `{rootElement.Name.LocalName}` when it should be `Decs`");
20✔
46
                }
20✔
47

48
                foreach (var decElement in rootElement.Elements())
130,535✔
49
                {
46,510✔
50
                    var readerDec = new ReaderDec();
46,510✔
51

52
                    readerDec.inputContext = new InputContext(fileIdentifier, decElement);
46,510✔
53
                    string typeName = decElement.Name.LocalName;
46,510✔
54

55
                    readerDec.type = UtilType.ParseDecFormatted(typeName, readerDec.inputContext);
46,510✔
56
                    if (readerDec.type == null || !typeof(Dec).IsAssignableFrom(readerDec.type))
46,510✔
57
                    {
40✔
58
                        Dbg.Err($"{readerDec.inputContext}: {typeName} is being used as a Dec but does not inherit from Dec.Dec");
40✔
59
                        continue;
40✔
60
                    }
61

62
                    var decNameAttribute = decElement.Attribute("decName");
46,470✔
63
                    if (decNameAttribute == null)
46,470✔
64
                    {
20✔
65
                        Dbg.Err($"{readerDec.inputContext}: No dec name provided, add a `decName=` attribute to the {typeName} tag (example: <{typeName} decName=\"TheNameOfYour{typeName}\">)");
20✔
66
                        continue;
20✔
67
                    }
68

69
                    readerDec.name = decNameAttribute.Value;
46,450✔
70
                    if (!UtilMisc.ValidateDecName(readerDec.name, readerDec.inputContext))
46,450✔
71
                    {
80✔
72
                        continue;
80✔
73
                    }
74

75
                    // Consume decName so we know it's not hanging around
76
                    decNameAttribute.Remove();
46,370✔
77

78
                    // Parse `class` if we can
79
                    if (decElement.Attribute("class") is var classAttribute && classAttribute != null)
46,370✔
80
                    {
70✔
81
                        var parsedClass = (Type)Serialization.ParseString(classAttribute.Value,
70✔
82
                            typeof(Type), null, readerDec.inputContext);
70✔
83

84
                        if (parsedClass == null)
70✔
85
                        {
20✔
86
                            // we have presumably already reported an error
87
                        }
20✔
88
                        else if (!readerDec.type.IsAssignableFrom(parsedClass))
50✔
89
                        {
20✔
90
                            Dbg.Err($"{readerDec.inputContext}: Attribute-parsed class {parsedClass} is not a subclass of {readerDec.type}; using the original class");
20✔
91
                        }
20✔
92
                        else
93
                        {
30✔
94
                            // yay
95
                            readerDec.type = parsedClass;
30✔
96
                        }
30✔
97

98
                        // clean up
99
                        classAttribute.Remove();
70✔
100
                    }
70✔
101

102
                    // Check to see if we're abstract
103
                    {
46,370✔
104
                        var abstractAttribute = decElement.Attribute("abstract");
46,370✔
105
                        if (abstractAttribute != null)
46,370✔
106
                        {
1,120✔
107
                            if (!bool.TryParse(abstractAttribute.Value, out bool abstrct))
1,120✔
108
                            {
20✔
109
                                Dbg.Err($"{readerDec.inputContext}: Error encountered when parsing abstract attribute");
20✔
110
                            }
20✔
111
                            readerDec.abstrct = abstrct; // little dance to deal with the fact that readerDec.abstrct is a `bool?`
1,120✔
112

113
                            abstractAttribute.Remove();
1,120✔
114
                        }
1,120✔
115
                    }
46,370✔
116

117
                    // Get our parent info
118
                    {
46,370✔
119
                        var parentAttribute = decElement.Attribute("parent");
46,370✔
120
                        if (parentAttribute != null)
46,370✔
121
                        {
1,300✔
122
                            readerDec.parent = parentAttribute.Value;
1,300✔
123

124
                            parentAttribute.Remove();
1,300✔
125
                        }
1,300✔
126
                    }
46,370✔
127

128
                    // Everything looks good!
129
                    readerDec.node = new ReaderNodeXml(decElement, fileIdentifier, userSettings);
46,370✔
130

131
                    result.Add(readerDec);
46,370✔
132
                }
46,370✔
133
            }
12,505✔
134

135
            return result;
12,505✔
136
        }
12,505✔
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc