// Copyright (c) 2019, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

const
    path = require('path'),
    CompilerDropinTool = require('../lib/tooling/compiler-dropin-tool');


describe('CompilerDropInTool', () => {
    it('Should support llvm based compilers', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/clang-8.0.0/bin/clang++",
                options: "--gcc-toolchain=/opt/compiler-explorer/gcc-7.2.0"
            },
            options: []
        };
        const includeflags = [];
        const args = [];
        const sourcefile = "example.cpp";

        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal(
            [
                "--gcc-toolchain=/opt/compiler-explorer/gcc-7.2.0",
                "--gcc-toolchain=/opt/compiler-explorer/gcc-7.2.0"
            ]
        );
    });

    it('Should support gcc based compilers', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/gcc-8.0/bin/g++",
                options: ""
            },
            options: []
        };
        const includeflags = [];
        const args = [];
        const sourcefile = "example.cpp";

        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal(
            [
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/gcc-8.0"),
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/gcc-8.0")
            ]
        );
    });

    it('Should support riscv gcc compilers', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/riscv64/gcc-8.2.0/riscv64-unknown-linux-gnu/bin/riscv64-unknown-linux-gnu-g++",
                options: ""
            },
            options: []
        };
        const includeflags = [];
        const args = [];
        const sourcefile = "example.cpp";

        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal(
            [
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/riscv64/gcc-8.2.0/riscv64-unknown-linux-gnu"),
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/riscv64/gcc-8.2.0/riscv64-unknown-linux-gnu")
            ]
        );
    });

    it('Should support ICC compilers', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/intel-2019.1/bin/icc",
                options: "--gxx-name=/opt/compiler-explorer/gcc-8.2.0/bin/g++"
            },
            options: []
        };
        const includeflags = [];
        const args = [];
        const sourcefile = "example.cpp";

        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal(
            [
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/gcc-8.2.0"),
                "--gcc-toolchain=" + path.resolve("/opt/compiler-explorer/gcc-8.2.0")
            ]
        );
    });

    it('Should default for WINE MSVC compilers', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/windows/19.14.26423/bin/cl.exe",
                options: 
                    "/I/opt/compiler-explorer/windows/10.0.10240.0/ucrt/ " +
                    "/I/opt/compiler-explorer/windows/19.14.26423/include/",
                internalIncludePaths: [
                    "/opt/compiler-explorer/windows/19.14.26423/include"
                ]
            },
            options: []
        };
        const includeflags = [];
        const args = ["/MD", "/STD:c++latest", "/Ox"];
        const sourcefile = "example.cpp";

        // todo: msvc includes are not readable by iwyu
        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal([
            "-I/opt/compiler-explorer/windows/19.14.26423/include"
        ]);
    });

    it('Should default even when using libc++', () => {
        const tool = new CompilerDropinTool({}, {});

        const compilationInfo = {
            compiler: {
                exe: "/opt/compiler-explorer/clang-concepts-trunk/bin/clang++",
                options: "-stdlib=libc++",
                internalIncludePaths: [
                    "/opt/compiler-explorer/clang-concepts-trunk/something/etc/include"
                ]
            },
            options: []
        };
        const includeflags = [];
        const args = [];
        const sourcefile = "example.cpp";

        const orderedArgs = tool.getOrderedArguments(compilationInfo, includeflags, args, sourcefile);
        orderedArgs.should.deep.equal(
            [
                "-I/opt/compiler-explorer/clang-concepts-trunk/something/etc/include"
            ]
        );
    });
});

@@ -3097,32 +3097,36 @@
 %0A    it('Should 
+not 
 support riscv gc
@@ -3708,286 +3708,13 @@
 ual(
-%0A            %5B%0A                %22--gcc-toolchain=%22 + path.resolve(%22/opt/compiler-explorer/riscv64/gcc-8.2.0/riscv64-unknown-linux-gnu%22),%0A                %22--gcc-toolchain=%22 + path.resolve(%22/opt/compiler-explorer/riscv64/gcc-8.2.0/riscv64-unknown-linux-gnu%22)%0A            %5D%0A        
+false
 );%0A 
@@ -4560,19 +4560,19 @@
 uld 
-default f
+not supp
 or
+t
  WIN
@@ -5284,64 +5284,8 @@
 %22;%0A%0A
-        // todo: msvc includes are not readable by iwyu%0A
     
@@ -5425,86 +5425,13 @@
 ual(
-%5B%0A            %22-I/opt/compiler-explorer/windows/19.14.26423/include%22%0A        %5D
+false
 );%0A 
@@ -5457,25 +5457,19 @@
 uld 
-default even when
+not support
  usi
@@ -6165,131 +6165,13 @@
 ual(
-%0A            %5B%0A                %22-I/opt/compiler-explorer/clang-concepts-trunk/something/etc/include%22%0A            %5D%0A        
+false
 );%0A 

fix tests