⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xptestedunitparsertests.pas

📁 For Delphi的单元测试工具DUnit的源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PublicMethodCount, PublicCount, Format('%s Public Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PublicMethodCount, PublicCount, Format('%s Public Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PublicMethodCount, PublicCount, Format('%s Public Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_ProtectedMethodCount, ProtectedCount, Format('%s Protected Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_ProtectedMethodCount, ProtectedCount, Format('%s Protected Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_ProtectedMethodCount, ProtectedCount, Format('%s Protected Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PrivateMethodCount, PrivateCount, Format('%s Private Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PrivateMethodCount, PrivateCount, Format('%s Private Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PrivateMethodCount, PrivateCount, Format('%s Private Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC6_Name]));

    end;

  end;

end;

procedure TXPTestedUnitParserTests.TestClassBoundaries;
var
  Node, ClassNode: IXPParserNode;
  SectionNode: IXPParserNode;
  LastEnd: longint;

begin
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin
    LastEnd := 0;
    SectionNode.Children.Start;

    while SectionNode.Children.Next(Node) do
      if Supports(Node, IXPClassNode, ClassNode) then
      begin
        Check((ClassNode as IXPClassNode).ClassEnd
          > (ClassNode as IXPClassNode).ClassBegin,
          Format('%s: Class length <= 0', [ClassNode.Name]));
        Check((ClassNode as IXPClassNode).ClassBegin > 0,
          Format('%s: Class begin < 0', [ClassNode.Name]));
        Check((ClassNode as IXPClassNode).ClassBegin > LastEnd,
          Format('%s: overlaps with previous class', [ClassNode.Name]));
        LastEnd := (ClassNode as IXPClassNode).ClassEnd;
      end;

  end;

end;

procedure TXPTestedUnitParserTests.TestClassUnitSections;
var
  Node: IXPParserNode;
  ClassNode: IXPClassNode;
  SectionNode: IXPParserNode;

begin
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin
    SectionNode.Children.Start;

    while SectionNode.Children.Next(Node) do
      if Supports(Node, IXPClassNode, ClassNode) then
      begin

        if ClassNode.Name = TSC1_Name then
          Check(usInterface = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC1_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usInterface]]));
        if ClassNode.Name = TSC2_Name then
          Check(usInterface = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC2_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usInterface]]));
        if ClassNode.Name = TSC3_Name then
          Check(usImplementation = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC3_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usImplementation]]));
        if ClassNode.Name = TSC4_Name then
          Check(usImplementation = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC4_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usImplementation]]));
        if ClassNode.Name = TSC5_Name then
          Check(usImplementation = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC5_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usImplementation]]));
        if ClassNode.Name = TSC6_Name then
          Check(usInterface = (ClassNode.Parent as IXPSectionNode).GetSection,
            Format('%s %s<>%s', [TSC6_Name,
              UnitSectionStrs[(ClassNode.Parent as IXPSectionNode).GetSection],
              UnitSectionStrs[usInterface]]));

      end;

  end;

end;

procedure TXPTestedUnitParserTests.TestPropertyByClassCount;
var
  SectionNode: IXPParserNode;
  ClassNode: IXPParserNode;
  VisibilityNode: IXPParserNode;
  Propertys: integer;

begin
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin
    SectionNode.Children.Start;

    while SectionNode.Children.Next(ClassNode) do
    begin
      Propertys := 0;
      ClassNode.Children.Start;

      while ClassNode.Children.Next(VisibilityNode) do
        System.Inc(Propertys, PropertyCount(VisibilityNode as IXPVisibilityNode));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PropertyCount, Propertys, Format('%s Count', [TSC1_Name]));

      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PropertyCount, Propertys, Format('%s Count', [TSC2_Name]));

      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PropertyCount, Propertys, Format('%s Count', [TSC3_Name]));

      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, Propertys, Format('%s Count', [TSC4_Name]));

      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, Propertys, Format('%s Count', [TSC5_Name]));

      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, Propertys, Format('%s Count', [TSC6_Name]));

    end;

  end;

end;

procedure TXPTestedUnitParserTests.TestPropertyByClassVisibilityCount;
var
  SectionNode: IXPParserNode;
  ClassNode: IXPParserNode;
  VisibilityNode: IXPParserNode;
  PrivateCount: integer;
  ProtectedCount: integer;
  PublicCount: integer;
  PublishedCount: integer;

begin
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin
    SectionNode.Children.Start;

    while SectionNode.Children.Next(ClassNode) do
    begin
      ClassNode.Children.Start;
      PrivateCount := 0;
      ProtectedCount := 0;
      PublicCount := 0;
      PublishedCount := 0;

      while ClassNode.Children.Next(VisibilityNode) do
      begin

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPrivate then
          PrivateCount := PropertyCount(VisibilityNode as IXPVisibilityNode);

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvProtected then
          ProtectedCount := PropertyCount(VisibilityNode as IXPVisibilityNode);

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPublic then
          PublicCount := PropertyCount(VisibilityNode as IXPVisibilityNode);

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPublished then
          PublishedCount := PropertyCount(VisibilityNode as IXPVisibilityNode);

      end;

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PublishedPropertyCount, PublishedCount, Format('%s Published Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PublishedPropertyCount, PublishedCount, Format('%s Published Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PublishedPropertyCount, PublishedCount, Format('%s Published Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PublishedCount, Format('%s Published Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PublicPropertyCount, PublicCount, Format('%s Public Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PublicPropertyCount, PublicCount, Format('%s Public Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PublicPropertyCount, PublicCount, Format('%s Public Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PublicCount, Format('%s Public Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_ProtectedPropertyCount, ProtectedCount, Format('%s Protected Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_ProtectedPropertyCount, ProtectedCount, Format('%s Protected Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_ProtectedPropertyCount, ProtectedCount, Format('%s Protected Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, ProtectedCount, Format('%s Protected Count', [TSC6_Name]));

      if ClassNode.Name = TSC1_Name then
        CheckEquals(TSC1_PrivatePropertyCount, PrivateCount, Format('%s Private Count', [TSC1_Name]));
      if ClassNode.Name = TSC2_Name then
        CheckEquals(TSC2_PrivatePropertyCount, PrivateCount, Format('%s Private Count', [TSC2_Name]));
      if ClassNode.Name = TSC3_Name then
        CheckEquals(TSC2_PrivatePropertyCount, PrivateCount, Format('%s Private Count', [TSC3_Name]));
      if ClassNode.Name = TSC4_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC4_Name]));
      if ClassNode.Name = TSC5_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC5_Name]));
      if ClassNode.Name = TSC6_Name then
        CheckEquals(0, PrivateCount, Format('%s Private Count', [TSC6_Name]));

    end;

  end;

end;

procedure TXPTestedUnitParserTests.TestPropertyByVisibilityCount;
var
  SectionNode: IXPParserNode;
  ClassNode: IXPParserNode;
  VisibilityNode: IXPVisibilityNode;
  PrivateCount: integer;
  ProtectedCount: integer;
  PublicCount: integer;
  PublishedCount: integer;

begin
  PrivateCount := 0;
  ProtectedCount := 0;
  PublicCount := 0;
  PublishedCount := 0;
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin
    SectionNode.Children.Start;

    while SectionNode.Children.Next(ClassNode) do
    begin
      ClassNode.Children.Start;

      while ClassNode.Children.Next(VisibilityNode) do
      begin

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPrivate then
          System.Inc(PrivateCount,
            PropertyCount(VisibilityNode as IXPVisibilityNode));

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvProtected then
          System.Inc(ProtectedCount,
            PropertyCount(VisibilityNode as IXPVisibilityNode));

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPublic then
          System.Inc(PublicCount,
            PropertyCount(VisibilityNode as IXPVisibilityNode));

        if (VisibilityNode as IXPVisibilityNode).GetVisibility = cvPublished then
          System.Inc(PublishedCount,
            PropertyCount(VisibilityNode as IXPVisibilityNode));

      end;

    end;

  end;
  CheckEquals(TotalPrivatePropertyCount, PrivateCount, 'TotalPrivateCount');
  CheckEquals(TotalProtectedPropertyCount, ProtectedCount, 'TotalProtectedCount');
  CheckEquals(TotalPublicPropertyCount, PublicCount, 'TotalPublicCount');
  CheckEquals(TotalPublishedPropertyCount, PublishedCount, 'TotalPublishedCount');
end;

function TXPTestedUnitParserTests.MethodCount(
  const AVisibilityNode: IXPVisibilityNode): integer;
var
  Child: IXPParserNode;
  MethodNode: IXPMethodNode;

begin
  Result := 0;
  AVisibilityNode.Children.Start;

  while AVisibilityNode.Children.Next(Child) do
    if Supports(Child, IXPMethodNode, MethodNode) then
      Inc(Result);
end;

function TXPTestedUnitParserTests.PropertyCount(
  const AVisibilityNode: IXPVisibilityNode): integer;
var
  Child: IXPParserNode;
  PropertyNode: IXPPropertyNode;

begin
  Result := 0;
  AVisibilityNode.Children.Start;

  while AVisibilityNode.Children.Next(Child) do
    if Supports(Child, IXPPropertyNode, PropertyNode) then
      Inc(Result);
end;

procedure TXPTestedUnitParserTests.TestGlobalFunctionCount;
var
  SectionNode: IXPParserNode;
  GlobalFunctionCount: integer;
  Node: IXPParserNode;
  FunctionNode: IXPFunctionNode;

begin
  GlobalFunctionCount := 0;
  FParser.Parse(FStream);
  FParser.ParseTree.Children.Start;

  while FParser.ParseTree.Children.Next(SectionNode) do
  begin

    SectionNode.Children.Start;

    while SectionNode.Children.Next(Node) do
    begin

      if Supports(Node, IXPFunctionNode, FunctionNode) then
        Inc(GlobalFunctionCount);

    end;

  end;

  CheckEquals(4, GlobalFunctionCount, 'TotalGlobalFunctionCount');
end;

initialization

  TestFramework.RegisterTest('XPTestedUnitParserTests Suite',
    TXPTestedUnitParserTests.Suite);

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -